Skip to content

Instantly share code, notes, and snippets.

@hamishdickson
Created January 16, 2017 16:48
Show Gist options
  • Save hamishdickson/89de9d020954ea2f10da5b5ce60af245 to your computer and use it in GitHub Desktop.
Save hamishdickson/89de9d020954ea2f10da5b5ce60af245 to your computer and use it in GitHub Desktop.
cofree impl of fib
final case class Cofree[F[_], A](head: A, tail: F[Cofree[F,A]])
val fibs: Cofree[Eval, Int] = {
def unfold(p1: Int, p2: Int): Cofree[Eval, Int] =
Cofree(p1 + p2, Eval.later(unfold(p2, p1 + p2)))
unfold(0, 1)
}
scala> fibs.head
res0: Int = 1
scala> fibs.tail.value
res3: Cofree[cats.Eval,Int] = Cofree(2,cats.Later@14cae9bc)
scala> fibs.tail.value.tail.value
res5: Cofree[cats.Eval,Int] = Cofree(3,cats.Later@4a46ec2a)
scala> fibs.tail.value.tail.value.tail.value
res6: Cofree[cats.Eval,Int] = Cofree(5,cats.Later@5e0774f7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment