Skip to content

Commit 2b66121

Browse files
Rename unfoldEachInterleave and unfoldEachRoundRobin
1 parent 3288ecf commit 2b66121

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

core/src/Streamly/Internal/Data/Stream/Nesting.hs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ module Streamly.Internal.Data.Stream.Nesting
8585
-- intercalate.
8686
, unfoldEachFoldBy
8787
, ConcatUnfoldInterleaveState (..)
88-
, unfoldEachInterleave -- bfsUnfoldEach
89-
, unfoldEachInterleaveRev -- altBfsUnfoldEach -- alternating directions
88+
, bfsUnfoldEach
89+
, altBfsUnfoldEach
9090

9191
-- *** unfoldEach joined by elements
9292
-- | Like unfoldEach but intersperses an element between the streams after
@@ -112,7 +112,7 @@ module Streamly.Internal.Data.Stream.Nesting
112112

113113
-- | Like unfoldEach but schedules the generated streams based on time
114114
-- slice instead of based on the outputs.
115-
, unfoldEachRoundRobin -- unfoldSched
115+
, unfoldSched
116116
-- , altUnfoldSched -- alternating directions
117117
-- , fairUnfoldSched
118118

@@ -198,6 +198,9 @@ module Streamly.Internal.Data.Stream.Nesting
198198
, parseIterateD
199199
, groupsBy
200200
, splitOnSeq
201+
, unfoldEachInterleave
202+
, unfoldEachInterleaveRev
203+
, unfoldEachRoundRobin
201204
)
202205
where
203206

@@ -764,23 +767,18 @@ data ConcatUnfoldInterleaveState o i =
764767
-- XXX Instead of using "reverse" build the list in the correct order to begin
765768
-- with.
766769

767-
-- | Like 'unfoldEach' but interleaves the resulting streams instead of
768-
-- appending them. Unfolds each element in the input stream to a stream and
769-
-- then interleave the resulting streams.
770+
-- | Like 'unfoldEach' but interleaves the resulting streams in a breadth first
771+
-- manner instead of appending them. Unfolds each element in the input stream
772+
-- to a stream and then interleave the resulting streams.
770773
--
771774
-- >>> lists = Stream.fromList [[1,4,7],[2,5,8],[3,6,9]]
772-
-- >>> Stream.toList $ Stream.unfoldEachInterleave Unfold.fromList lists
775+
-- >>> Stream.toList $ Stream.bfsUnfoldEach Unfold.fromList lists
773776
-- [1,2,3,4,5,6,7,8,9]
774777
--
775-
-- This is similar to 'mergeMapWith' using 'Streamly.Data.StreamK.interleave'
776-
-- but an order of magnitude more efficient due to fusion.
777-
--
778-
-- See also 'mergeMapWith'.
779-
--
780-
{-# INLINE_NORMAL unfoldEachInterleave #-}
781-
unfoldEachInterleave :: Monad m =>
778+
{-# INLINE_NORMAL bfsUnfoldEach #-}
779+
bfsUnfoldEach, unfoldEachInterleave :: Monad m =>
782780
Unfold m a b -> Stream m a -> Stream m b
783-
unfoldEachInterleave (Unfold istep inject) (Stream ostep ost) =
781+
bfsUnfoldEach (Unfold istep inject) (Stream ostep ost) =
784782
Stream step (ConcatUnfoldInterleaveOuter ost [])
785783

786784
where
@@ -825,18 +823,21 @@ unfoldEachInterleave (Unfold istep inject) (Stream ostep ost) =
825823
Skip s -> Skip (ConcatUnfoldInterleaveInnerR ls (s:rs))
826824
Stop -> Skip (ConcatUnfoldInterleaveInnerR ls rs)
827825

828-
-- | Like 'unfoldEachInterleave' but reverses the traversal direction after
829-
-- reaching the last stream. This could be little bit more efficient if the
830-
-- order of traversal is not important.
826+
RENAME(unfoldEachInterleave, bfsUnfoldEach)
827+
828+
-- | Like 'bfsUnfoldEach' but reverses the traversal direction after reaching
829+
-- the last stream and then after reaching the first stream, thus alternating
830+
-- the directions. This could be a little bit more efficient if the order of
831+
-- traversal is not important.
831832
--
832833
-- >>> lists = Stream.fromList [[1,4,7],[2,5,8],[3,6,9]]
833-
-- >>> Stream.toList $ Stream.unfoldEachInterleaveRev Unfold.fromList lists
834+
-- >>> Stream.toList $ Stream.altBfsUnfoldEach Unfold.fromList lists
834835
-- [1,2,3,6,5,4,7,8,9]
835836
--
836-
{-# INLINE_NORMAL unfoldEachInterleaveRev #-}
837-
unfoldEachInterleaveRev, unfoldInterleave :: Monad m =>
837+
{-# INLINE_NORMAL altBfsUnfoldEach #-}
838+
altBfsUnfoldEach, unfoldEachInterleaveRev, unfoldInterleave :: Monad m =>
838839
Unfold m a b -> Stream m a -> Stream m b
839-
unfoldEachInterleaveRev (Unfold istep inject) (Stream ostep ost) =
840+
altBfsUnfoldEach (Unfold istep inject) (Stream ostep ost) =
840841
Stream step (ConcatUnfoldInterleaveOuter ost [])
841842

842843
where
@@ -881,7 +882,8 @@ unfoldEachInterleaveRev (Unfold istep inject) (Stream ostep ost) =
881882
Skip s -> Skip (ConcatUnfoldInterleaveInnerR ls (s:rs))
882883
Stop -> Skip (ConcatUnfoldInterleaveInnerR ls rs)
883884

884-
RENAME(unfoldInterleave,unfoldEachInterleaveRev)
885+
RENAME(unfoldInterleave,altBfsUnfoldEach)
886+
RENAME(unfoldEachInterleaveRev,altBfsUnfoldEach)
885887

886888
-- XXX In general we can use different scheduling strategies e.g. how to
887889
-- schedule the outer vs inner loop or assigning weights to different streams
@@ -891,16 +893,18 @@ RENAME(unfoldInterleave,unfoldEachInterleaveRev)
891893
--
892894
-- Compared to unfoldEachInterleave this one switches streams on Skips.
893895

894-
-- | 'unfoldEachInterleave' switches to the next stream whenever a value from a
896+
-- | 'bfdUnfoldEach' switches to the next stream whenever a value from a
895897
-- stream is yielded, it does not switch on a 'Skip'. So if a stream keeps
896898
-- skipping for long time other streams won't get a chance to run.
897-
-- 'unfoldEachRoundRobin' switches on Skip as well. So it basically schedules each
899+
-- 'unfoldSched' switches on Skip as well. So it basically schedules each
898900
-- stream fairly irrespective of whether it produces a value or not.
899901
--
900-
{-# INLINE_NORMAL unfoldEachRoundRobin #-}
901-
unfoldEachRoundRobin, unfoldRoundRobin :: Monad m =>
902+
-- An N-ary version of 'roundRobin'.
903+
--
904+
{-# INLINE_NORMAL unfoldSched #-}
905+
unfoldSched, unfoldEachRoundRobin, unfoldRoundRobin :: Monad m =>
902906
Unfold m a b -> Stream m a -> Stream m b
903-
unfoldEachRoundRobin (Unfold istep inject) (Stream ostep ost) =
907+
unfoldSched (Unfold istep inject) (Stream ostep ost) =
904908
Stream step (ConcatUnfoldInterleaveOuter ost [])
905909
where
906910
{-# INLINE_LATE step #-}
@@ -945,7 +949,8 @@ unfoldEachRoundRobin (Unfold istep inject) (Stream ostep ost) =
945949
Skip s -> Skip (ConcatUnfoldInterleaveInnerR (s:ls) rs)
946950
Stop -> Skip (ConcatUnfoldInterleaveInnerR ls rs)
947951

948-
RENAME(unfoldRoundRobin,unfoldEachRoundRobin)
952+
RENAME(unfoldRoundRobin,unfoldSched)
953+
RENAME(unfoldEachRoundRobin,unfoldSched)
949954

950955
-- | Round robin co-operative scheduling of multiple streams.
951956
--

0 commit comments

Comments
 (0)