I have a lazily calculated sequence of objects where a lazy calculation depends only on the index (and not on the previous elements) and some constant parameters ( p:Bar below). I am currently using Stream , however calculating stream.init usually wasteful.
However, I really like that using Stream[Foo] = ... saves me from implementing the cache and has very easy declaration syntax while still providing all the sugar (for example, stream(n) gets the element n ). Again, I could just use the wrong declaration:
class FooSrcCache(p:Bar) { val src : Stream[FooSrc] = { def error() : FooSrc = FooSrc(0,p) def loop(i: Int): Stream[FooSrc] = { FooSrc(i,p) #:: loop(i + 1) } error() #:: loop(1) } def apply(max: Int) = src(max) }
Is there a Stream Scala base class that is indexed instead of linear?
source share