|
| 1 | + |
| 2 | +## Motivation |
| 3 | + |
| 4 | +Consider the following program that finds the second prime number between 1000 and 10000: |
| 5 | + |
| 6 | + ((1000 to 10000) filter isPrime)(1) |
| 7 | + |
| 8 | +This is *much* shorter than the recursive alternative: |
| 9 | + |
| 10 | + |
| 11 | + def nthPrime(from: Int, to: Int, n: Int): Int = |
| 12 | + if (from >= to) throw new Error("no prime") |
| 13 | + else if (isPrime(from)) |
| 14 | + if (n == 1) from else nthPrime(from + 1, to, n - 1) |
| 15 | + else nthPrime(from + 1, to, n) |
| 16 | + |
| 17 | + def secondPrime(from: Int, to: Int) = nthPrime(from, to, 2) |
| 18 | + |
| 19 | +But from a standpoint of performance, the first version is pretty bad; it constructs |
| 20 | +*all* prime numbers between `1000` and `10000` in a list, but only ever looks at |
| 21 | +the first two elements of that list. |
| 22 | + |
| 23 | +Reducing the upper bound would speed things up, but risks that we miss the |
| 24 | +second prime number altogether. |
| 25 | + |
| 26 | +## Delayed Evaluation |
| 27 | + |
| 28 | +However, we can make the short-code efficient by using a trick: |
| 29 | + |
| 30 | +- Avoid computing the tail of a sequence until it is needed for the evaluation |
| 31 | + result (which might be never) |
| 32 | + |
| 33 | +This idea is implemented in a new class, the `LazyList`. |
| 34 | + |
| 35 | +LazyLists are similar to lists, but their elements are evaluated only ''on demand''. |
| 36 | + |
| 37 | +## Defining LazyLists |
| 38 | + |
| 39 | +LazyLists are defined from a constructor `LazyList.cons`. |
| 40 | + |
| 41 | +For instance, |
| 42 | + |
| 43 | + val xs = LazyList.cons(1, LazyList.cons(2, LazyList.empty)) |
| 44 | + |
| 45 | +## LazyList Ranges |
| 46 | + |
| 47 | +Let's try to write a function that returns a `LazyList` representing a range of numbers |
| 48 | +between `lo` and `hi`: |
| 49 | + |
| 50 | + def llRange(lo: Int, hi: Int): LazyList[Int] = |
| 51 | + if (lo >= hi) LazyList.empty |
| 52 | + else LazyList.cons(lo, llRange(lo + 1, hi)) |
| 53 | + |
| 54 | +Compare to the same function that produces a list: |
| 55 | + |
| 56 | + def listRange(lo: Int, hi: Int): List[Int] = |
| 57 | + if (lo >= hi) Nil |
| 58 | + else lo :: listRange(lo + 1, hi) |
| 59 | + |
| 60 | +The functions have almost identical structure yet they evaluate quite differently. |
| 61 | + |
| 62 | +- `listRange(start, end)` will produce a list with `end - start` elements and return it. |
| 63 | +- `llRange(start, end)` returns a single object of type `LazyList` with `start` as head element. |
| 64 | + - The other elements are only computed when they are needed, where “needed” means that someone calls `tail` on the stream. |
| 65 | + |
| 66 | +## Methods on LazyLists |
| 67 | + |
| 68 | +`LazyList` supports almost all methods of `List`. |
| 69 | + |
| 70 | +For instance, to find the second prime number between 1000 and 10000: |
| 71 | + |
| 72 | + (llRange(1000, 10000) filter isPrime)(1) |
| 73 | + |
| 74 | +The one major exception is `::`. |
| 75 | + |
| 76 | +`x :: xs` always produces a list, never a lazy list. |
| 77 | + |
| 78 | +There is however an alternative operator `#::` which produces a lazy list. |
| 79 | + |
| 80 | + x #:: xs == LazyList.cons(x, xs) |
| 81 | + |
| 82 | +`#::` can be used in expressions as well as patterns. |
| 83 | + |
| 84 | +## Implementation of LazyLists |
| 85 | + |
| 86 | +The implementation of lazy lists is quite close to the one of lists. |
| 87 | + |
| 88 | +Here's the class `LazyList`: |
| 89 | + |
| 90 | + final class LazyList[+A] ... extends ... { |
| 91 | + override def isEmpty: Boolean = ... |
| 92 | + override def head: A = ... |
| 93 | + override def tail: LazyList[A] = ... |
| 94 | + … |
| 95 | + } |
| 96 | + |
| 97 | +As for lists, all other methods can be defined in terms of these three. |
| 98 | + |
| 99 | +Concrete implementations of streams are defined in the `LazyList.State` companion object. |
| 100 | +Here's a first draft: |
| 101 | + |
| 102 | + private object State { |
| 103 | + object Empty extends State[Nothing] { |
| 104 | + def head: Nothing = throw new NoSuchElementException("head of empty lazy list") |
| 105 | + def tail: LazyList[Nothing] = throw new UnsupportedOperationException("tail of empty lazy list") |
| 106 | + } |
| 107 | + |
| 108 | + final class Cons[A](val head: A, val tail: LazyList[A]) extends State[A] |
| 109 | + } |
| 110 | + |
| 111 | +The only important difference between the implementations of `List` and `LazyList` |
| 112 | +concern `tail`, the second parameter of `LazyList.cons`. |
| 113 | + |
| 114 | +For lazy lists, this is a by-name parameter: the type of `tail` starts with `=>`. In such |
| 115 | +a case, this parameter is evaluated by following the rules of the call-by-name model. |
| 116 | + |
| 117 | +That's why the second argument to `LazyList.cons` is not evaluated at the point of call. |
| 118 | + |
| 119 | +Instead, it will be evaluated each time someone calls `tail` on a `LazyList` object. |
| 120 | + |
| 121 | +In Scala 2.13, LazyList (previously Stream) became fully lazy from head to tail. To make it possible, |
| 122 | +methods (`filter`, `flatMap`...) are implemented in a way where the head is not being evaluated if is |
| 123 | +not explicitly indicated. |
| 124 | + |
| 125 | +For instance, here's `filter`: |
| 126 | + |
| 127 | + object LazyList extends SeqFactory[LazyList] { |
| 128 | + … |
| 129 | + private def filterImpl[A](ll: LazyList[A], p: A => Boolean, isFlipped: Boolean): LazyList[A] = { |
| 130 | + // DO NOT REFERENCE `ll` ANYWHERE ELSE, OR IT WILL LEAK THE HEAD |
| 131 | + var restRef = ll // val restRef = new ObjectRef(ll) |
| 132 | + newLL { |
| 133 | + var elem: A = null.asInstanceOf[A] |
| 134 | + var found = false |
| 135 | + var rest = restRef // var rest = restRef.elem |
| 136 | + while (!found && !rest.isEmpty) { |
| 137 | + elem = rest.head |
| 138 | + found = p(elem) != isFlipped |
| 139 | + rest = rest.tail |
| 140 | + restRef = rest // restRef.elem = rest |
| 141 | + } |
| 142 | + if (found) sCons(elem, filterImpl(rest, p, isFlipped)) else State.Empty |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +## Exercise |
| 147 | + |
| 148 | +Consider the modification of `llRange` given in the code editor. When you write |
| 149 | +`llRange(1, 10).take(3).toList` what is the value of `rec`? |
| 150 | + |
| 151 | +Be careful, head is evaluating too! |
0 commit comments