Skip to content

Instantly share code, notes, and snippets.

@evantill
Last active March 8, 2018 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantill/a547e96cfd599f6cf296883afd4999f0 to your computer and use it in GitHub Desktop.
Save evantill/a547e96cfd599f6cf296883afd4999f0 to your computer and use it in GitHub Desktop.
State for comprehension sample
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