I have a list of streams List[Stream[_]]
, size of list is known at the beginning of function, size of each stream equals n
or n+1
. I'd like to obtain interleave stream
e.g.
def myMagicFold[A](s: List[Stream[A]]): Stream[A]
val streams = List(Stream(1,1,1),Stream(2,2,2),Stream(3,3),Stream(4,4))
val result = myMagicFold(streams)
//result = Stream(1,2,3,4,1,2,3,4,1,2)
I'm using fs2.Stream
. My first take:
val result = streams.fold(fs2.Stream.empty){
case (s1, s2) => s1.interleaveAll(s2)
}
// result = Stream(1, 4, 3, 4, 2, 3, 1, 2, 1, 2)
I'm looking for a solution based on basic operations (map
, fold
,...)