title: Infinite streams in Scala
body: <p>Say I have a function, for example the old favourite</p>
  
  <pre><code>def factorial(n:Int) = (BigInt(1) /: (1 to n)) (_*_)
  </code></pre>
  
  <p>Now I want to find the biggest value of <code>n</code> for which <code>factorial(n)</code> fits in a Long. I could do </p>
  
  <pre><code>(1 to 100) takeWhile (factorial(_) &lt;= Long.MaxValue) last
  </code></pre>
  
  <p>This works, but the 100 is an arbitrary large number; what I really want on the left hand side is an infinite stream that keeps generating higher numbers until the <code>takeWhile</code> condition is met.</p>
  
  <p>I've come up with </p>
  
  <pre><code>val s = Stream.continually(1).zipWithIndex.map(p =&gt; p._1 + p._2)
  </code></pre>
  
  <p>but is there a better way?</p>
  
  <p>(I'm also aware I could get a solution recursively but that's not what I'm looking for.)</p>