Created
September 6, 2015 20:47
-
-
Save cem2ran/0e46744122e5fa4ea216 to your computer and use it in GitHub Desktop.
Revisions
-
cem2ran created this gist
Sep 6, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ def unfold[A,S](initial: S)(generateNext: S => Option[(A, S)]): Stream[A] = generateNext(initial) match { case Some((first, next)) => cons(first, unfold(next)(generateNext)) case None => empty } val fibs = unfold((0,1)) { case (f0, f1) => Some((f0, (f1, f0+f1))) } def from(n: Int) = unfold(n)(n => Some(n, n+1)) def constant[A](a: A) = unfold(a)(_ => Some((a, a)))