I want a convenient way to generate an Iterable , a given starting object and a function, to create the next object from the current one that consumes O (1) memory (i.e. does not cache the old results; if you want to iterate a second time, the function must be applied again )
The library does not seem to support the library. In Scala 2.8, the scala.collection.Iterable.iterate method has a signature
def iterate [A] (start: A, len: Int)(f: (A) ⇒ A) : Iterable[A]
therefore, it is required that you indicate how many iterative functional applications you are interested in in advance, and my understanding of the documentation is that Iterable.iterate actually calculates all these values immediately. The scala.collection.Iterator.iterate method, on the other hand, has a signature
def iterate [T] (start: T)(f: (T) ⇒ T) : Iterator[T]
which looks great, but we only get an Iterator that does not offer all the amenities of map , filter and friends.
Is there a convenient library method for creating what I want?
And if not,
Can anyone suggest a "colloquial" Scala code for this?
To summarize, given the starting object a: A and the function f: A => A , I would like to use a TraversableLike (e.g., possibly Iterable ) that generates a, f(a), f(f(a)), ... and uses O (1) memory, with map , filter , etc. functions that also return what is O (1) in memory.
Scott Morrison
source share