Skip to content
Snippets Groups Projects
Commit 3c3a0338 authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[lec04] Stream is now LazyList

parent cd95001c
No related branches found
No related tags found
No related merge requests found
package laziness
sealed trait Stream[+A] {
sealed trait LazyList[+A] {
// uncomment to be able to use cons(h, t) and empty directly
// imports methods from the companion object
//import Stream._
//import LazyList._
def toList: List[A] = ???
def take(n: Int): Stream[A] = ???
def take(n: Int): LazyList[A] = ???
//This can be tailrecursive. Uncomment below to let the compiler check
//This can be tailrecursive. Uncomment below to let the compiler check for it.
//@annotation.tailrec
def drop(n: Int): Stream[A] = ???
def drop(n: Int): LazyList[A] = ???
// Methods shown during lecture
......@@ -32,35 +32,35 @@ sealed trait Stream[+A] {
}
}
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
case object Empty extends LazyList[Nothing]
case class Cons[+A](h: () => A, t: () => LazyList[A]) extends LazyList[A]
object Stream { // companion object
object LazyList { // companion object
def fibs: Stream[Int] = {
def fibs: LazyList[Int] = {
// tip: write a recursive def here and call it with some start values
???
}
def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = ???
def unfold[A, S](z: S)(f: S => Option[(A, S)]): LazyList[A] = ???
def fibsViaUnfold: Stream[Int] = ???
def fibsViaUnfold: LazyList[Int] = ???
// Methods shown during Lecture
def cons[A](h: => A, t: => Stream[A]): Stream[A] = {
def cons[A](h: => A, t: => LazyList[A]): LazyList[A] = {
lazy val head = h
lazy val tail = t
Cons(() => head, () => tail)
}
def empty[A]: Stream[A] = Empty
def empty[A]: LazyList[A] = Empty
def apply[A](as: A*): Stream[A] =
def apply[A](as: A*): LazyList[A] =
if (as.isEmpty) empty
else cons(as.head, apply(as.tail: _*))
val ones: Stream[Int] = cons(1, ones)
val ones: LazyList[Int] = cons(1, ones)
}
......@@ -3,18 +3,18 @@ package laziness
import org.scalatest._
import testutil.PendingIfUnimplemented
class StreamSpec extends FlatSpec with Matchers with AppendedClues with PendingIfUnimplemented {
class LazyListSpec extends FlatSpec with Matchers with AppendedClues with PendingIfUnimplemented {
import Stream._
import LazyList._
/*
* Note that equivalence checking on streams does not work.
* Note that equivalence checking on lazy lists does not work.
* Therefore it is important, that your toList implementation works for most later tests.
*/
"A stream" should "be convertable to a list" in {
Stream(1, 2, 3).toList shouldBe List(1, 2, 3)
Stream.empty.toList shouldBe List()
"A LazyList" should "be convertable to an eagerly evaluated list" in {
LazyList(1, 2, 3).toList shouldBe List(1, 2, 3)
LazyList.empty.toList shouldBe List()
}
it should "have a take method that keeps only a certain number of elements" in {
......@@ -24,7 +24,7 @@ class StreamSpec extends FlatSpec with Matchers with AppendedClues with PendingI
}
it should "have a drop method that removes a certain number of elements from the start" in {
Stream(1, 2, 3, 4).drop(1).toList shouldBe List(2, 3, 4)
LazyList(1, 2, 3, 4).drop(1).toList shouldBe List(2, 3, 4)
ones.drop(17).headOption should contain(1)
}
......@@ -33,7 +33,7 @@ class StreamSpec extends FlatSpec with Matchers with AppendedClues with PendingI
fibs.take(8).toList shouldBe List(0, 1, 1, 2, 3, 5, 8, 13)
}
"unfold" should "should generate streams from a function while stopping on None" in {
"unfold" should "should generate lazy lists from a function while stopping on None" in {
val alphabet = unfold('a')(s => if(s <= 'z') Some((s, (s+1).toChar)) else None)
alphabet.toList shouldBe ('a' to 'z').toList
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment