on master help sign in
Rename Flatten module to Combine.
5551242Pranay Sashank
src/Streamly/Internal/Data/Stream/StreamD.hs+1−1
@@ -311,6 +311,6 @@ import Streamly.Internal.Data.Stream.StreamD.Type
311import Streamly.Internal.Data.Stream.StreamD.Generate
311import Streamly.Internal.Data.Stream.StreamD.Generate
312import Streamly.Internal.Data.Stream.StreamD.Eliminate
312import Streamly.Internal.Data.Stream.StreamD.Eliminate
313import Streamly.Internal.Data.Stream.StreamD.Exceptions
313import Streamly.Internal.Data.Stream.StreamD.Exceptions
314−import Streamly.Internal.Data.Stream.StreamD.Flatten
314+import Streamly.Internal.Data.Stream.StreamD.Combine
315import Streamly.Internal.Data.Stream.StreamD.SplitGroup
315import Streamly.Internal.Data.Stream.StreamD.SplitGroup
316import Streamly.Internal.Data.Stream.StreamD.Transform
316import 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 @@
1#include "inline.hs"
1#include "inline.hs"
2
2
3-- |
3-- |
4−-- Module : Streamly.Internal.Data.Stream.StreamD.Flatten
4+-- Module : Streamly.Internal.Data.Stream.StreamD.Combine
5-- Copyright : (c) 2018 Composewell Technologies
5-- Copyright : (c) 2018 Composewell Technologies
6-- (c) Roman Leshchinskiy 2008-2010
6-- (c) Roman Leshchinskiy 2008-2010
7-- (c) The University of Glasgow, 2009
7-- (c) The University of Glasgow, 2009
@@ -10,10 +10,20 @@
10-- Stability : experimental
10-- Stability : experimental
11-- Portability : GHC
11-- Portability : GHC
12
12
13−module Streamly.Internal.Data.Stream.StreamD.Flatten
13+module Streamly.Internal.Data.Stream.StreamD.Combine
14 (
14 (
15+ -- * Zipping
16+ indexed
17+ , indexedR
18+ , zipWith
19+ , zipWithM
20+
21+ -- * Merging
22+ , mergeBy
23+ , mergeByM
24+
15 -- ** Flattening nested streams
25 -- ** Flattening nested streams
16− concatMapM
26+ , concatMapM
17 , concatMap
27 , concatMap
18 , ConcatMapUState (..)
28 , ConcatMapUState (..)
19 , concatMapU
29 , concatMapU
@@ -35,6 +45,9 @@ module Streamly.Internal.Data.Stream.StreamD.Flatten
35 )
45 )
36where
46where
37
47
48+#if __GLASGOW_HASKELL__ >= 801
49+import Data.Functor.Identity ( Identity )
50+#endif
38import Fusion.Plugin.Types (Fuse(..))
51import Fusion.Plugin.Types (Fuse(..))
39import Streamly.Internal.Data.Unfold.Types (Unfold(..))
52import Streamly.Internal.Data.Unfold.Types (Unfold(..))
40
53
@@ -48,6 +61,117 @@ import Prelude hiding
48import Streamly.Internal.Data.Stream.StreamD.Type
61import Streamly.Internal.Data.Stream.StreamD.Type
49import Streamly.Internal.Data.SVar
62import Streamly.Internal.Data.SVar
50
63
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+
51-------------------------------------------------------------------------------
175-------------------------------------------------------------------------------
52-- Stream transformations using Unfolds
176-- Stream transformations using Unfolds
53-------------------------------------------------------------------------------
177-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/StreamD/Transform.hs−121
@@ -92,20 +92,10 @@ module Streamly.Internal.Data.Stream.StreamD.Transform
92 , mapMaybe
92 , mapMaybe
93 , mapMaybeM
93 , mapMaybeM
94
94
95− -- * Zipping
96− , indexed
97− , indexedR
98− , zipWith
99− , zipWithM
100−
101 -- * Parsers
95 -- * Parsers
102 , parseMany
96 , parseMany
103 , parseIterate
97 , parseIterate
104
98
105− -- * Merging
106− , mergeBy
107− , mergeByM
108−
109 -- * Concurrent Application
99 -- * Concurrent Application
110 , mkParallel
100 , mkParallel
111 , mkParallelD
101 , mkParallelD
@@ -1135,67 +1125,6 @@ mapMaybe f = fmap fromJust . filter isJust . map f
1135mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
1125mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
1136mapMaybeM f = fmap fromJust . filter isJust . mapM f
1126mapMaybeM f = fmap fromJust . filter isJust . mapM f
1137
1127
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−
1199------------------------------------------------------------------------------
1128------------------------------------------------------------------------------
1200-- Repeated parsing
1129-- Repeated parsing
1201------------------------------------------------------------------------------
1130------------------------------------------------------------------------------
@@ -1417,56 +1346,6 @@ parseIterate func seed (Stream step state) =
1417
1346
1418 stepOuter _ (ConcatParseYield a next) = return $ Yield a next
1347 stepOuter _ (ConcatParseYield a next) = return $ Yield a next
1419
1348
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−
1470-------------------------------------------------------------------------------
1349-------------------------------------------------------------------------------
1471-- Concurrent application and fold
1350-- Concurrent application and fold
1472-------------------------------------------------------------------------------
1351-------------------------------------------------------------------------------
streamly.cabal+1−1
@@ -455,7 +455,7 @@ library
455 , Streamly.Internal.Data.Stream.StreamD.Generate
455 , Streamly.Internal.Data.Stream.StreamD.Generate
456 , Streamly.Internal.Data.Stream.StreamD.SplitGroup
456 , Streamly.Internal.Data.Stream.StreamD.SplitGroup
457 , Streamly.Internal.Data.Stream.StreamD.Eliminate
457 , Streamly.Internal.Data.Stream.StreamD.Eliminate
458− , Streamly.Internal.Data.Stream.StreamD.Flatten
458+ , Streamly.Internal.Data.Stream.StreamD.Combine
459 , Streamly.Internal.Data.Stream.StreamD.Transform
459 , Streamly.Internal.Data.Stream.StreamD.Transform
460 , Streamly.Internal.Data.Stream.StreamD.Exceptions
460 , Streamly.Internal.Data.Stream.StreamD.Exceptions
461 , Streamly.Internal.Data.Stream.StreamDK.Type
461 , Streamly.Internal.Data.Stream.StreamDK.Type