Skip to content

Instantly share code, notes, and snippets.

@markosski
Created December 21, 2018 01:08
Show Gist options
  • Save markosski/b1e34e32a959ea845e9c904871eec925 to your computer and use it in GitHub Desktop.
Save markosski/b1e34e32a959ea845e9c904871eec925 to your computer and use it in GitHub Desktop.
thunks
sealed trait Stream[+A]
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
object Stream {
def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
lazy val head = hd
lazy val tail = tl
Cons(() => head, () => tail)
}
def empty[A]: Stream[A] = Empty
def apply[A](as: A*): Stream[A] =
if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment