Suppose I have a generator that returns me some results, broken into pieces, that I want to pull into a flat list:
def pull(chunk: Chunk, result: Stream[Item] = Stream.empty): Stream[Item] = { val soFar = chunk.items ++ result if(chunk.hasNext) pull(generator.next(chunk), soFar) else soFar }
Conceptually, this is what I want, in addition, it extracts all the content in advance, and I want it to be lazy. Something like that:
Stream.iterate(generator.first)(generator.next) .takeWhile(_.hasNext) .flatMap(_.items)
almost works, but he discards the last piece.
I seem to need .takeUntil here: like takeWhile , but go through the whole chain before ending. How can I do this idiomatically?
source share