Skip to content

Instantly share code, notes, and snippets.

@cem2ran
Created September 6, 2015 20:47
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 cem2ran/0e46744122e5fa4ea216 to your computer and use it in GitHub Desktop.
Save cem2ran/0e46744122e5fa4ea216 to your computer and use it in GitHub Desktop.
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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment