I am doing an exercise on the scala stream. I had from the book the following code. (I wrote the toList function)
trait Stream2[+A] {
def uncons: Option[(A, Stream2[A])]
def isEmpty: Boolean = uncons.isEmpty
def toList: List[A] = {
def toListAcc(l:List[A], s:Stream2[A]):List[A]= {
s.uncons match {
case Some((h, t)) => toListAcc(List(h) ++ l, t)
case None => l
}
}
toListAcc(List(), this).reverse
}
def take(n: Int): Stream[A] = {
???}
}
object Stream2 {
def empty[A]: Stream2[A] =
new Stream2[A] { def uncons = None }
def cons[A](hd: => A, tl: => Stream2[A]): Stream2[A] =
new Stream2[A] {
lazy val uncons = Some((hd, tl))
}
def apply[A](as: A*): Stream2[A] =
if (as.isEmpty) empty
else cons(as.head, apply(as.tail: _*))
}
val s = Stream2(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) //> s : chapter5.Stream2[Int] = chapter5$$anonfun$main$1$Stream2$3$$anon$2@782a
//| fee5
s.toList //> res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
}
Now I have to write the take function that creates a stream with only the first n elements. I am totally lost and I do not understand what do to. I think that my problems are due to the fact that I did not fully understand how a Stream2 object is created. I guess that this is related to the uncons
function ( or maybe cons
).
So finally I would like:
1) an explanation of how this code should work
2) To know if toList is written correctly in term of way to dial with lazy evaluation
3) the code for the function take or at least some hints to write it by myself.