Skip to content

Instantly share code, notes, and snippets.

@sderosiaux
Created November 28, 2017 22: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 sderosiaux/42103f75b265c6b14f641b6069784673 to your computer and use it in GitHub Desktop.
Save sderosiaux/42103f75b265c6b14f641b6069784673 to your computer and use it in GitHub Desktop.
Trampoline using Cats
def value(i: List[Int]): Trampoline[List[Int]] = i match {
case Nil => Free.pure(Nil)
case head :: tail => Free.defer(value(tail)).flatMap { l => Free.pure(head + 1 :: l) }
}
import cats.implicits._
println(value((1 to 10).toList).run) // List(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment