I want a convenient way to generate an Iterable
, given a initial object and a function to produce the next object from the current one, that consumes O(1) memory (i.e., it doesn't cache old results; if you want to iterate a second time, the function has to be applied again).
It doesn't seem like there's library support for this. In Scala 2.8, the method scala.collection.Iterable.iterate
has signature
def iterate [A] (start: A, len: Int)(f: (A) ⇒ A) : Iterable[A]
so it requires that you specify how many iterated function applications you're interested in ahead of time, and my understanding of the documentation is that Iterable.iterate
actually computes all these values immediately. On the other hand, the method scala.collection.Iterator.iterate
has signature
def iterate [T] (start: T)(f: (T) ⇒ T) : Iterator[T]
which looks great, but we only get an Iterator
which doesn't offer all the convenience of map
, filter
and friends.
Is there a convenient library method to produce what I want?
and if not,
Can someone suggest the 'colloquial' Scala code for doing this?
To summarise, given an initial object a: A
, and a function f: A => A
, I'd like a TraversableLike
(e.g., probably an Iterable
) that generates a, f(a), f(f(a)), ...
, and uses O(1) memory, with map
, filter
etc. functions that also return something that is O(1) in memory.