Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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: _*))
- def headOption: Option[A] = this match {
- case Empty => None
- case Cons(h, t) => Some(h())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement