Created
December 21, 2018 01:08
-
-
Save markosski/b1e34e32a959ea845e9c904871eec925 to your computer and use it in GitHub Desktop.
thunks
This file contains 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
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