Last active
March 8, 2018 09:40
-
-
Save evantill/a547e96cfd599f6cf296883afd4999f0 to your computer and use it in GitHub Desktop.
State for comprehension sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package draft | |
import arrow.* | |
import arrow.core.* | |
import arrow.data.* | |
import arrow.syntax.option.* | |
import arrow.typeclasses.* | |
import arrow.instances.* | |
typealias Stack = Option<Nel<String>> | |
fun pop() = State<Stack, Option<String>> { stack -> | |
stack.fold({ | |
None toT None | |
}, { | |
Nel.fromList(it.tail) toT it.head.some() | |
}) | |
} | |
fun push(s: String) = State<Stack, Unit> { stack -> | |
stack.fold({ | |
Nel.of(s).some() toT Unit | |
}, { | |
Nel(s, it.all).some() toT Unit | |
}) | |
} | |
fun stackOperations() = State().monad<Stack>().binding { | |
val a = push("a").bind() | |
val b = pop().bind() | |
val c = pop().bind() | |
c | |
}.fix() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment