title: How to use LazyList and takeWhile or dropWhile and avoid producing a never-ending stream?
body: <p>My goal is to create an infinite stream of state objects where each successive object modifies itself, and the stream terminates when a certain state is reached. For example, in the sample below, the state object just increments itself, and I want to get the last object before 100.</p>
  <pre><code>object LazyListTest extends App {
    val test = LazyListTest()
    val last: LazyListTest = test.playout.takeWhile(_.a &lt; 100).last
  }
  
  case class LazyListTest(a: Int = 0, maybePrev: Option[LazyListTest] = None) {
    def playout: LazyList[LazyListTest] = this #:: playout.map {
      n =&gt; n.increment
    }
    def increment: LazyListTest = {
      copy(a + 1, maybePrev = Some(this))
    }
  }
  </code></pre>
  <p>In the example above, I expected to get an object back with <code>a == 99</code>. Instead, the list never ends. When it hits 99, the stream just repeats starting back at 0. (Which is another question, what is creating this new object?)</p>
  <p>When I use <code>dropWhile</code>, the stream also repeats infinitely, but it goes beyond 99, repeating at an incremental value above 99 (e.g. 101, 102, ...).</p>