Last active — forked from travisbrown/trampolined-state.scala

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Trampolining the state monad via applicative combinator appears to work

View trampolined-state.scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import scalaz._, Scalaz._
def setS(i: Int): State[List[Int], Unit] = modify(i :: _)
val s = (1 to 10000).foldLeft(state[List[Int], Unit](()).lift[Free.Trampoline]) {
case (st, i) => st *> setS(i).lift[Free.Trampoline]
}
s(Nil).run
 
// Purposely choosing not to traverse here for demonstration purposes
val s2 = (1 to 10000).foldLeft(state[List[Int], List[Unit]](List.empty).lift[Free.Trampoline]) {
case (st, i) => (st |@| setS(i).lift[Free.Trampoline])((xs, x) => x :: xs)
}
 
s2(Nil).run
 
val s3 = (1 to 10000).toList.traverseU {
i => setS(i).lift[Free.Trampoline]
}
 
s3(Nil).run
Owner

For some reason the applicative works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.