Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Created June 24, 2015 14:33
Show Gist options
  • Save gigiigig/2ad29f29f6a2fb88313c to your computer and use it in GitHub Desktop.
Save gigiigig/2ad29f29f6a2fb88313c to your computer and use it in GitHub Desktop.
Scalaz StateT Trampolines
package console
import scalaz._, Scalaz._
object console extends App {
def countList[T](l: List[T]): Int = {
val i: State[Int, Int] = init[Int]
(l.foldLeft(init[Int].lift[Free.Trampoline]) { (acc, elem) =>
for {
_ <- acc
f <- modify((i: Int) => i + 1).lift[Free.Trampoline]
v <- get.lift[Free.Trampoline]
} yield v
}).exec(0).run
}
val res = countList((1 to 10000).toList)
println(s"res: ${res}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment