A valid IndexedSeq stream equivalent?

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?

+4
source share
1 answer

PagedSeq should do the job for you:

 class FooSrcCache(p:Bar) { private def fill(buf: Array[FooSrc], start: Int, end: Int) = { for (i <- start until end) { buf(i) = FooSrc(i,p) } end - start } val src = new PagedSeq[FooSrc](fill _) def apply(max: Int) = src(max) } 

Please note that this can calculate FooSrc with higher scores than you requested.

+3
source

All Articles