@@ -486,7 +486,8 @@ data ConcatUnfoldInterleaveState o i =
486486 | ConcatUnfoldInterleaveInnerL [i] [i]
487487 | ConcatUnfoldInterleaveInnerR [i] [i]
488488
489−-- XXX use arrays to store state instead of lists.
489+-- XXX use arrays to store state instead of lists?
490+--
490491-- XXX In general we can use different scheduling strategies e.g. how to
491492-- schedule the outer vs inner loop or assigning weights to different streams
492493-- or outer and inner loops.
@@ -502,11 +503,31 @@ data ConcatUnfoldInterleaveState o i =
502503-- Ideally, we need some scheduling bias to inner streams vs outer stream.
503504-- Maybe we can configure the behavior.
504505--
506+-- XXX Instead of using "concatPairsWith wSerial" we can implement an N-way
507+-- interleaving CPS combinator which behaves like unfoldManyInterleave. Intead
508+-- of pairing up the streams We just need to go yielding one element from each
509+-- stream and storing the remaining streams and then keep doing rounds through
510+-- those in a round robin fashion. This would be much like wAsync.
511+--
512+-- | This does not pair streams like concatPairsWith, instead, it goes through
513+-- each stream one by one and yields one element from each stream. After it
514+-- goes to the last stream it reverses the traversal to come back to the first
515+-- stream yielding elements from each stream on its way back to the first
516+-- stream and so on.
517+--
518+-- >>> input = Stream.fromList [[1,1],[2,2],[3,3],[4,4],[5,5]]
519+-- >>> Stream.toList $ Stream.unfoldManyInterleave Unfold.fromList input
520+-- [1,2,3,4,5,5,4,3,2,1]
521+--
522+-- Note that this is order of magnitude more efficient than "concatPairsWith
523+-- wSerial"
505524{-# INLINE_NORMAL unfoldManyInterleave #-}
506525unfoldManyInterleave :: Monad m => Unfold m a b -> Stream m a -> Stream m b
507526unfoldManyInterleave (Unfold istep inject) (Stream ostep ost) =
508527 Stream step (ConcatUnfoldInterleaveOuter ost [])
509− where
528+
529+ where
530+
510531 {-# INLINE_LATE step #-}
511532 step gst (ConcatUnfoldInterleaveOuter o ls) = do
512533 r <- ostep (adaptState gst) o