TakeUntil? Processing tagged input. How?

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?

+5
source share
1 answer

This is what I came up with ... Looks like yucky, but this is the best I could come up with:

  Stream.iterate(generator.first) { case chunk if chunk.hasNext => generator.next case _ => null }.takeWhile(_ != null) .flatMap(_.items) 

Any better ideas?

0
source

Source: https://habr.com/ru/post/1262002/


All Articles