Recipe: Changing Seq[A] to A

Sometimes you have an "external world" process that gives you a Seq[A] but you need to process the data one A at a time.

You can define a pipe (until it comes standard in the library) like:

val toA = pipe.lift { seq => Stream.chunk(Chunk.seq(seq)) }

then you can do:

yourSourceOfSeqA.through(toA).toVector

This is equivalant to the following and is shorter:

yourSourceOfSeqA.flatMap(Stream.emits)

Last updated