Created
June 24, 2015 14:33
-
-
Save gigiigig/2ad29f29f6a2fb88313c to your computer and use it in GitHub Desktop.
Scalaz StateT Trampolines
This file contains 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 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