diff --git a/src/main/scala/laziness/Stream.scala b/src/main/scala/laziness/LazyList.scala similarity index 60% rename from src/main/scala/laziness/Stream.scala rename to src/main/scala/laziness/LazyList.scala index ba1ab97687392363f45fa05f12f6779fd3e5cd50..a9fcc7b8847c8ba82494b923a55843da7e6af837 100644 --- a/src/main/scala/laziness/Stream.scala +++ b/src/main/scala/laziness/LazyList.scala @@ -1,17 +1,17 @@ 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) } diff --git a/src/test/scala/laziness/StreamSpec.scala b/src/test/scala/laziness/LazyListSpec.scala similarity index 65% rename from src/test/scala/laziness/StreamSpec.scala rename to src/test/scala/laziness/LazyListSpec.scala index 8942521c8696fbf1ba3cead00619a1772a5c8d28..680d5f3502dc925fccea23f9b7b66c01bcb90802 100644 --- a/src/test/scala/laziness/StreamSpec.scala +++ b/src/test/scala/laziness/LazyListSpec.scala @@ -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 }