Skip to content

Commit 20fb611

Browse files
Rename concatMapInterleave and concatMapDiagonal
From more technical and longer to more accessible and shorter ones.
1 parent d888b90 commit 20fb611

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

core/src/Streamly/Data/StreamK.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ module Streamly.Data.StreamK
177177
, bindWith
178178
, concatMapWith
179179
, mergeMapWith
180-
, concatMapInterleave
181-
, concatMapDiagonal
180+
-- , bfsConcatMap
181+
-- , fairConcatMap
182182

183183
-- * Buffered Operations
184184
, reverse

core/src/Streamly/Internal/Data/StreamK/Type.hs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ module Streamly.Internal.Data.StreamK.Type
134134
, concatMapWith
135135
, bindWith
136136
, concatMap
137-
, concatMapInterleave
138-
, concatMapDiagonal
137+
, bfsConcatMap
138+
, fairConcatMap
139139
, concatMapMAccum
140140
, concatIterateWith
141141
, concatIterateLeftsWith
@@ -1471,7 +1471,7 @@ bindWith combine m1 f = go m1
14711471
-- >>> un $ StreamK.concatMapWith StreamK.interleave mk lists
14721472
-- [1,2,5,3,6,4,7,8]
14731473
--
1474-
-- For a fair interleaving example see 'concatMapInterleave' and 'mergeMapWith'.
1474+
-- For a fair interleaving example see 'bfsConcatMap' and 'mergeMapWith'.
14751475
--
14761476
{-# INLINE concatMapWith #-}
14771477
concatMapWith
@@ -1607,7 +1607,7 @@ mergeMapWith combine f str = go (leafPairs str)
16071607
-- XXX check if bindInterleave has better perf like bindWith?
16081608

16091609
-- | While concatMap flattens a stream of streams in a depth first manner,
1610-
-- 'concatMapInterleave' flattens it in a breadth-first manner. It yields one
1610+
-- 'bfsConcatMap' flattens it in a breadth-first manner. It yields one
16111611
-- item from the first stream, then one item from the next stream and so on.
16121612
-- Given a stream of three streams:
16131613
--
@@ -1622,7 +1622,7 @@ mergeMapWith combine f str = go (leafPairs str)
16221622
-- For example:
16231623
--
16241624
-- >>> stream = mk [[1,2,3],[4,5,6],[7,8,9]]
1625-
-- >>> un $ StreamK.concatMapInterleave mk stream
1625+
-- >>> un $ StreamK.bfsConcatMap mk stream
16261626
-- [1,4,7,2,5,8,3,6,9]
16271627
--
16281628
-- Compare with 'concatMapWith' 'interleave' which explores the depth
@@ -1631,12 +1631,12 @@ mergeMapWith combine f str = go (leafPairs str)
16311631
--
16321632
-- See also the equivalent fused version 'Data.Stream.unfoldEachInterleave'.
16331633
--
1634-
{-# INLINE concatMapInterleave #-}
1635-
concatMapInterleave ::
1634+
{-# INLINE bfsConcatMap #-}
1635+
bfsConcatMap ::
16361636
(a -> StreamK m b)
16371637
-> StreamK m a
16381638
-> StreamK m b
1639-
concatMapInterleave f m1 = go id m1
1639+
bfsConcatMap f m1 = go id m1
16401640

16411641
where
16421642

@@ -1682,7 +1682,7 @@ concatMapInterleave f m1 = go id m1
16821682
yieldk a r = yld a (goLoop (ys . (r :)) xs)
16831683
foldStream st yieldk single stop x
16841684

1685-
-- | 'concatMapDiagonal' flattens a stream of streams in a diagonal manner.
1685+
-- | 'fairConcatMap' flattens a stream of streams in a diagonal manner.
16861686
-- Every previous stream yields one more item than the next. Therefore, the
16871687
-- depth and breadth of traversal is equally balanced.
16881688
-- Given a stream of three streams:
@@ -1701,19 +1701,19 @@ concatMapInterleave f m1 = go id m1
17011701
-- For example:
17021702
--
17031703
-- >>> stream = mk [[1,2,3],[4,5,6],[7,8,9]]
1704-
-- >>> un $ StreamK.concatMapDiagonal mk stream
1704+
-- >>> un $ StreamK.fairConcatMap mk stream
17051705
-- [1,2,4,3,5,7,6,8,9]
17061706
--
17071707
-- Compare with 'concatMapWith interleave' which explores the depth
17081708
-- exponentially more compared to the breadth, such that each stream yields
17091709
-- twice as many items compared to the next stream.
17101710
--
1711-
{-# INLINE concatMapDiagonal #-}
1712-
concatMapDiagonal ::
1711+
{-# INLINE fairConcatMap #-}
1712+
fairConcatMap ::
17131713
(a -> StreamK m b)
17141714
-> StreamK m a
17151715
-> StreamK m b
1716-
concatMapDiagonal f m1 = go id m1
1716+
fairConcatMap f m1 = go id m1
17171717

17181718
where
17191719

@@ -2008,9 +2008,9 @@ infixr 6 `interleave`
20082008
-- streams will be opened at any given time, because the first stream will
20092009
-- finish by the time the stream after @log n@ th stream is opened.
20102010
--
2011-
-- Compare with 'concatMapInterleave' and 'mergeMapWith' 'interleave'.
2011+
-- Compare with 'bfsConcatMap' and 'mergeMapWith' 'interleave'.
20122012
--
2013-
-- For interleaving many streams, the best way is to use 'concatMapInterleave'.
2013+
-- For interleaving many streams, the best way is to use 'bfsConcatMap'.
20142014
--
20152015
-- See also the fused version 'Streamly.Data.Stream.interleave'.
20162016
{-# INLINE interleave #-}
@@ -2518,13 +2518,13 @@ concatMapEffect f action =
25182518
-- 'Nested' also serves the purpose of 'LogicT' type from the 'logict' package.
25192519
-- The @MonadLogic@ operations can be implemented using the available stream
25202520
-- operations. For example, 'uncons' is @msplit@, 'interleave' corresponds to
2521-
-- the @interleave@ operation of MonadLogic, 'concatMapDiagonal' is a flipped
2521+
-- the @interleave@ operation of MonadLogic, 'fairConcatMap' is a flipped
25222522
-- fair bind (@>>-@) operation. The 'FairNested' type provides a monad with fair
25232523
-- bind.
25242524
--
25252525
-- == Related Functionality
25262526
--
2527-
-- A custom type can be created using 'concatMapInterleave' as the monad bind
2527+
-- A custom type can be created using 'bfsConcatMap' as the monad bind
25282528
-- operation then the nested loops would get inverted - the innermost loop
25292529
-- becomes the outermost and vice versa.
25302530
--
@@ -2647,7 +2647,10 @@ instance (MonadThrow m) => MonadThrow (Nested m) where
26472647

26482648
-- XXX We can fix the termination issues by adding a "skip" continuation in the
26492649
-- stream. Adding a "block" continuation can allow for blocking IO. Both of
2650-
-- these together will provide a co-operative scheduling.
2650+
-- these together will provide a co-operative scheduling. However, adding skip
2651+
-- will regress performance in heavy filtering cases. If that's important we
2652+
-- create another type StreamK' for skip continuation. That type can use
2653+
-- conversion from Stream type for everything except append and concatMap.
26512654

26522655
-- | 'FairNested' is like the 'Nested' type but explores the depth and breadth of
26532656
-- the cross product grid equally, so that each of the stream being crossed is
@@ -2704,7 +2707,7 @@ instance (MonadThrow m) => MonadThrow (Nested m) where
27042707
--
27052708
-- == How it works?
27062709
--
2707-
-- 'FairNested' uses flipped 'concatMapDiagonal' as the monad bind operation.
2710+
-- 'FairNested' uses flipped 'fairConcatMap' as the monad bind operation.
27082711
-- If we look at the cross product of [1,2,3], [4,5,6], the streams being
27092712
-- combined using 'concatMapDigaonal' are the sequential loop iterations:
27102713
--
@@ -2863,13 +2866,10 @@ instance Monad m => Monad (FairNested m) where
28632866
return = pure
28642867

28652868
{-# INLINE (>>=) #-}
2866-
(>>=) (FairNested m) f = FairNested (concatMapDiagonal (unFairNested . f) m)
2867-
-- (>>=) (FairNested m) f = FairNested (concatMapInterleave (unFairNested . f) m)
2868-
-- (>>=) (FairNested m) f = FairNested (concatMapWith interleave (unFairNested . f) m)
2869-
-- (>>=) (FairNested m) f = FairNested (mergeMapWith interleave (unFairNested . f) m)
2869+
(>>=) (FairNested m) f = FairNested (fairConcatMap (unFairNested . f) m)
28702870

2871-
{-# INLINE (>>) #-}
2872-
(>>) = (*>)
2871+
-- {-# INLINE (>>) #-}
2872+
-- (>>) = (*>)
28732873

28742874
------------------------------------------------------------------------------
28752875
-- Transformers

0 commit comments

Comments
 (0)