on master help sign in
Rename Flatten module to Combine.

- Zipping & merging ops are moved from StreamD.Transform to StreamD.Combine module.

commit55512420094cef7ef29e3cf5f343d6d0da1df1b0 authorPranay Sashank <pranaysashank@composewell.com> dateSun 3 Jan 2021, 04:24 (6 years ago) parent97d5dc6
4 files changed+129−126
src/Streamly/Internal/Data/Stream/StreamD.hs+1−1 src/Streamly/Internal/Data/Stream/StreamD/Flatten.hs → src/Streamly/Internal/Data/Stream/StreamD/Combine.hs+127−3 src/Streamly/Internal/Data/Stream/StreamD/Transform.hs−121 streamly.cabal+1−1
src/Streamly/Internal/Data/Stream/StreamD.hs+1−1
@@ -311,6 +311,6 @@ import Streamly.Internal.Data.Stream.StreamD.Type
311311import Streamly.Internal.Data.Stream.StreamD.Generate
312312import Streamly.Internal.Data.Stream.StreamD.Eliminate
313313import Streamly.Internal.Data.Stream.StreamD.Exceptions
314−import Streamly.Internal.Data.Stream.StreamD.Flatten
314+import Streamly.Internal.Data.Stream.StreamD.Combine
315315import Streamly.Internal.Data.Stream.StreamD.SplitGroup
316316import Streamly.Internal.Data.Stream.StreamD.Transform
src/Streamly/Internal/Data/Stream/StreamD/Flatten.hs → src/Streamly/Internal/Data/Stream/StreamD/Combine.hsrenamed+127−3
@@ -1,7 +1,7 @@
11#include "inline.hs"
22
33-- |
4−-- Module : Streamly.Internal.Data.Stream.StreamD.Flatten
4+-- Module : Streamly.Internal.Data.Stream.StreamD.Combine
55-- Copyright : (c) 2018 Composewell Technologies
66-- (c) Roman Leshchinskiy 2008-2010
77-- (c) The University of Glasgow, 2009
@@ -10,10 +10,20 @@
1010-- Stability : experimental
1111-- Portability : GHC
1212
13−module Streamly.Internal.Data.Stream.StreamD.Flatten
13+module Streamly.Internal.Data.Stream.StreamD.Combine
1414 (
15+ -- * Zipping
16+ indexed
17+ , indexedR
18+ , zipWith
19+ , zipWithM
20+
21+ -- * Merging
22+ , mergeBy
23+ , mergeByM
24+
1525 -- ** Flattening nested streams
16− concatMapM
26+ , concatMapM
1727 , concatMap
1828 , ConcatMapUState (..)
1929 , concatMapU
@@ -35,6 +45,9 @@ module Streamly.Internal.Data.Stream.StreamD.Flatten
3545 )
3646where
3747
48+#if __GLASGOW_HASKELL__ >= 801
49+import Data.Functor.Identity ( Identity )
50+#endif
3851import Fusion.Plugin.Types (Fuse(..))
3952import Streamly.Internal.Data.Unfold.Types (Unfold(..))
4053
@@ -48,6 +61,117 @@ import Prelude hiding
4861import Streamly.Internal.Data.Stream.StreamD.Type
4962import Streamly.Internal.Data.SVar
5063
64+------------------------------------------------------------------------------
65+-- Zipping
66+------------------------------------------------------------------------------
67+
68+{-# INLINE_NORMAL indexed #-}
69+indexed :: Monad m => Stream m a -> Stream m (Int, a)
70+indexed (Stream step state) = Stream step' (state, 0)
71+ where
72+ {-# INLINE_LATE step' #-}
73+ step' gst (st, i) = i `seq` do
74+ r <- step (adaptState gst) st
75+ case r of
76+ Yield x s -> return $ Yield (i, x) (s, i+1)
77+ Skip s -> return $ Skip (s, i)
78+ Stop -> return Stop
79+
80+{-# INLINE_NORMAL indexedR #-}
81+indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
82+indexedR m (Stream step state) = Stream step' (state, m)
83+ where
84+ {-# INLINE_LATE step' #-}
85+ step' gst (st, i) = i `seq` do
86+ r <- step (adaptState gst) st
87+ case r of
88+ Yield x s -> let i' = i - 1
89+ in return $ Yield (i, x) (s, i')
90+ Skip s -> return $ Skip (s, i)
91+ Stop -> return Stop
92+
93+{-# INLINE_NORMAL zipWithM #-}
94+zipWithM :: Monad m
95+ => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
96+zipWithM f (Stream stepa ta) (Stream stepb tb) = Stream step (ta, tb, Nothing)
97+ where
98+ {-# INLINE_LATE step #-}
99+ step gst (sa, sb, Nothing) = do
100+ r <- stepa (adaptState gst) sa
101+ return $
102+ case r of
103+ Yield x sa' -> Skip (sa', sb, Just x)
104+ Skip sa' -> Skip (sa', sb, Nothing)
105+ Stop -> Stop
106+
107+ step gst (sa, sb, Just x) = do
108+ r <- stepb (adaptState gst) sb
109+ case r of
110+ Yield y sb' -> do
111+ z <- f x y
112+ return $ Yield z (sa, sb', Nothing)
113+ Skip sb' -> return $ Skip (sa, sb', Just x)
114+ Stop -> return Stop
115+
116+#if __GLASGOW_HASKELL__ >= 801
117+{-# RULES "zipWithM xs xs"
118+ forall f xs. zipWithM @Identity f xs xs = mapM (\x -> f x x) xs #-}
119+#endif
120+
121+{-# INLINE zipWith #-}
122+zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
123+zipWith f = zipWithM (\a b -> return (f a b))
124+
125+------------------------------------------------------------------------------
126+-- Merging
127+------------------------------------------------------------------------------
128+
129+{-# INLINE_NORMAL mergeByM #-}
130+mergeByM
131+ :: (Monad m)
132+ => (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
133+mergeByM cmp (Stream stepa ta) (Stream stepb tb) =
134+ Stream step (Just ta, Just tb, Nothing, Nothing)
135+ where
136+ {-# INLINE_LATE step #-}
137+
138+ -- one of the values is missing, and the corresponding stream is running
139+ step gst (Just sa, sb, Nothing, b) = do
140+ r <- stepa gst sa
141+ return $ case r of
142+ Yield a sa' -> Skip (Just sa', sb, Just a, b)
143+ Skip sa' -> Skip (Just sa', sb, Nothing, b)
144+ Stop -> Skip (Nothing, sb, Nothing, b)
145+
146+ step gst (sa, Just sb, a, Nothing) = do
147+ r <- stepb gst sb
148+ return $ case r of
149+ Yield b sb' -> Skip (sa, Just sb', a, Just b)
150+ Skip sb' -> Skip (sa, Just sb', a, Nothing)
151+ Stop -> Skip (sa, Nothing, a, Nothing)
152+
153+ -- both the values are available
154+ step _ (sa, sb, Just a, Just b) = do
155+ res <- cmp a b
156+ return $ case res of
157+ GT -> Yield b (sa, sb, Just a, Nothing)
158+ _ -> Yield a (sa, sb, Nothing, Just b)
159+
160+ -- one of the values is missing, corresponding stream is done
161+ step _ (Nothing, sb, Nothing, Just b) =
162+ return $ Yield b (Nothing, sb, Nothing, Nothing)
163+
164+ step _ (sa, Nothing, Just a, Nothing) =
165+ return $ Yield a (sa, Nothing, Nothing, Nothing)
166+
167+ step _ (Nothing, Nothing, Nothing, Nothing) = return Stop
168+
169+{-# INLINE mergeBy #-}
170+mergeBy
171+ :: (Monad m)
172+ => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
173+mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
174+
51175-------------------------------------------------------------------------------
52176-- Stream transformations using Unfolds
53177-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/StreamD/Transform.hs−121
@@ -92,20 +92,10 @@ module Streamly.Internal.Data.Stream.StreamD.Transform
9292 , mapMaybe
9393 , mapMaybeM
9494
95− -- * Zipping
96− , indexed
97− , indexedR
98− , zipWith
99− , zipWithM
100−
10195 -- * Parsers
10296 , parseMany
10397 , parseIterate
10498
105− -- * Merging
106− , mergeBy
107− , mergeByM
108−
10999 -- * Concurrent Application
110100 , mkParallel
111101 , mkParallelD
@@ -1135,67 +1125,6 @@ mapMaybe f = fmap fromJust . filter isJust . map f
11351125mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
11361126mapMaybeM f = fmap fromJust . filter isJust . mapM f
11371127
1138−------------------------------------------------------------------------------
1139−-- Zipping
1140−------------------------------------------------------------------------------
1141−
1142−{-# INLINE_NORMAL indexed #-}
1143−indexed :: Monad m => Stream m a -> Stream m (Int, a)
1144−indexed (Stream step state) = Stream step' (state, 0)
1145− where
1146− {-# INLINE_LATE step' #-}
1147− step' gst (st, i) = i `seq` do
1148− r <- step (adaptState gst) st
1149− case r of
1150− Yield x s -> return $ Yield (i, x) (s, i+1)
1151− Skip s -> return $ Skip (s, i)
1152− Stop -> return Stop
1153−
1154−{-# INLINE_NORMAL indexedR #-}
1155−indexedR :: Monad m => Int -> Stream m a -> Stream m (Int, a)
1156−indexedR m (Stream step state) = Stream step' (state, m)
1157− where
1158− {-# INLINE_LATE step' #-}
1159− step' gst (st, i) = i `seq` do
1160− r <- step (adaptState gst) st
1161− case r of
1162− Yield x s -> let i' = i - 1
1163− in return $ Yield (i, x) (s, i')
1164− Skip s -> return $ Skip (s, i)
1165− Stop -> return Stop
1166−
1167−{-# INLINE_NORMAL zipWithM #-}
1168−zipWithM :: Monad m
1169− => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
1170−zipWithM f (Stream stepa ta) (Stream stepb tb) = Stream step (ta, tb, Nothing)
1171− where
1172− {-# INLINE_LATE step #-}
1173− step gst (sa, sb, Nothing) = do
1174− r <- stepa (adaptState gst) sa
1175− return $
1176− case r of
1177− Yield x sa' -> Skip (sa', sb, Just x)
1178− Skip sa' -> Skip (sa', sb, Nothing)
1179− Stop -> Stop
1180−
1181− step gst (sa, sb, Just x) = do
1182− r <- stepb (adaptState gst) sb
1183− case r of
1184− Yield y sb' -> do
1185− z <- f x y
1186− return $ Yield z (sa, sb', Nothing)
1187− Skip sb' -> return $ Skip (sa, sb', Just x)
1188− Stop -> return Stop
1189−
1190−#if __GLASGOW_HASKELL__ >= 801
1191−{-# RULES "zipWithM xs xs"
1192− forall f xs. zipWithM @Identity f xs xs = mapM (\x -> f x x) xs #-}
1193−#endif
1194−
1195−{-# INLINE zipWith #-}
1196−zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
1197−zipWith f = zipWithM (\a b -> return (f a b))
1198−
11991128------------------------------------------------------------------------------
12001129-- Repeated parsing
12011130------------------------------------------------------------------------------
@@ -1417,56 +1346,6 @@ parseIterate func seed (Stream step state) =
14171346
14181347 stepOuter _ (ConcatParseYield a next) = return $ Yield a next
14191348
1420−------------------------------------------------------------------------------
1421−-- Merging
1422−------------------------------------------------------------------------------
1423−
1424−{-# INLINE_NORMAL mergeByM #-}
1425−mergeByM
1426− :: (Monad m)
1427− => (a -> a -> m Ordering) -> Stream m a -> Stream m a -> Stream m a
1428−mergeByM cmp (Stream stepa ta) (Stream stepb tb) =
1429− Stream step (Just ta, Just tb, Nothing, Nothing)
1430− where
1431− {-# INLINE_LATE step #-}
1432−
1433− -- one of the values is missing, and the corresponding stream is running
1434− step gst (Just sa, sb, Nothing, b) = do
1435− r <- stepa gst sa
1436− return $ case r of
1437− Yield a sa' -> Skip (Just sa', sb, Just a, b)
1438− Skip sa' -> Skip (Just sa', sb, Nothing, b)
1439− Stop -> Skip (Nothing, sb, Nothing, b)
1440−
1441− step gst (sa, Just sb, a, Nothing) = do
1442− r <- stepb gst sb
1443− return $ case r of
1444− Yield b sb' -> Skip (sa, Just sb', a, Just b)
1445− Skip sb' -> Skip (sa, Just sb', a, Nothing)
1446− Stop -> Skip (sa, Nothing, a, Nothing)
1447−
1448− -- both the values are available
1449− step _ (sa, sb, Just a, Just b) = do
1450− res <- cmp a b
1451− return $ case res of
1452− GT -> Yield b (sa, sb, Just a, Nothing)
1453− _ -> Yield a (sa, sb, Nothing, Just b)
1454−
1455− -- one of the values is missing, corresponding stream is done
1456− step _ (Nothing, sb, Nothing, Just b) =
1457− return $ Yield b (Nothing, sb, Nothing, Nothing)
1458−
1459− step _ (sa, Nothing, Just a, Nothing) =
1460− return $ Yield a (sa, Nothing, Nothing, Nothing)
1461−
1462− step _ (Nothing, Nothing, Nothing, Nothing) = return Stop
1463−
1464−{-# INLINE mergeBy #-}
1465−mergeBy
1466− :: (Monad m)
1467− => (a -> a -> Ordering) -> Stream m a -> Stream m a -> Stream m a
1468−mergeBy cmp = mergeByM (\a b -> return $ cmp a b)
1469−
14701349-------------------------------------------------------------------------------
14711350-- Concurrent application and fold
14721351-------------------------------------------------------------------------------
streamly.cabal+1−1
@@ -455,7 +455,7 @@ library
455455 , Streamly.Internal.Data.Stream.StreamD.Generate
456456 , Streamly.Internal.Data.Stream.StreamD.SplitGroup
457457 , Streamly.Internal.Data.Stream.StreamD.Eliminate
458− , Streamly.Internal.Data.Stream.StreamD.Flatten
458+ , Streamly.Internal.Data.Stream.StreamD.Combine
459459 , Streamly.Internal.Data.Stream.StreamD.Transform
460460 , Streamly.Internal.Data.Stream.StreamD.Exceptions
461461 , Streamly.Internal.Data.Stream.StreamDK.Type