Created
January 16, 2017 16:48
-
-
Save hamishdickson/89de9d020954ea2f10da5b5ce60af245 to your computer and use it in GitHub Desktop.
cofree impl of fib
This file contains hidden or 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
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