@@ -93,13 +93,14 @@ import qualified Prelude
93import qualified System.IO as IO
93import qualified System.IO as IO
95import Streamly.Core
95import Streamly.Core
96−import Streamly.Streams
96+import Streamly.Streams hiding (runStream)
97------------------------------------------------------------------------------
98------------------------------------------------------------------------------
98-- Construction
99-- Construction
99------------------------------------------------------------------------------
100------------------------------------------------------------------------------
101-- | Build a Stream by unfolding pure steps starting from a seed.
102-- | Build a Stream by unfolding pure steps starting from a seed.
102−unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a
103+unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
103unfoldr step = fromStream . go
104unfoldr step = fromStream . go
105 go s = Stream $ \_ stp yld ->
106 go s = Stream $ \_ stp yld ->
@@ -108,7 +109,7 @@ unfoldr step = fromStream . go
108 Just (a, b) -> yld a (Just (go b))
109 Just (a, b) -> yld a (Just (go b))
110-- | Build a Stream by unfolding monadic steps starting from a seed.
111-- | Build a Stream by unfolding monadic steps starting from a seed.
111−unfoldrM :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
112+unfoldrM :: (IsStream t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
112unfoldrM step = fromStream . go
113unfoldrM step = fromStream . go
114 go s = Stream $ \_ stp yld -> do
115 go s = Stream $ \_ stp yld -> do
@@ -120,17 +121,17 @@ unfoldrM step = fromStream . go
120-- XXX need eachInterleaved, eachAsync, eachParallel
121-- XXX need eachInterleaved, eachAsync, eachParallel
121-- | Same as @foldWith (<>)@ but more efficient.
122-- | Same as @foldWith (<>)@ but more efficient.
122{-# INLINE each #-}
123{-# INLINE each #-}
123−each :: (Streaming t, Foldable f) => f a -> t m a
124+each :: (IsStream t, Foldable f) => f a -> t m a
124each = Prelude.foldr cons nil
125each = Prelude.foldr cons nil
126-- | Iterate a pure function from a seed value, streaming the results forever
127-- | Iterate a pure function from a seed value, streaming the results forever
127−iterate :: Streaming t => (a -> a) -> a -> t m a
128+iterate :: IsStream t => (a -> a) -> a -> t m a
128iterate step = fromStream . go
129iterate step = fromStream . go
130 go s = scons s (Just (go (step s)))
131 go s = scons s (Just (go (step s)))
132-- | Iterate a monadic function from a seed value, streaming the results forever
133-- | Iterate a monadic function from a seed value, streaming the results forever
133−iterateM :: (Streaming t, Monad m) => (a -> m a) -> a -> t m a
134+iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
134iterateM step = fromStream . go
135iterateM step = fromStream . go
136 go s = Stream $ \_ _ yld -> do
137 go s = Stream $ \_ _ yld -> do
@@ -138,7 +139,7 @@ iterateM step = fromStream . go
138 yld s (Just (go a))
139 yld s (Just (go a))
140-- | Read lines from an IO Handle into a stream of Strings.
141-- | Read lines from an IO Handle into a stream of Strings.
141−fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
142+fromHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String
142fromHandle h = fromStream go
143fromHandle h = fromStream go
144 go = Stream $ \_ stp yld -> do
145 go = Stream $ \_ stp yld -> do
@@ -156,7 +157,7 @@ fromHandle h = fromStream go
156-- Parallel variants of folds?
157-- Parallel variants of folds?
158-- | Right fold.
159-- | Right fold.
159−foldr :: (Streaming t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
160+foldr :: (IsStream t, Monad m) => (a -> b -> b) -> b -> t m a -> m b
160foldr step acc m = go (toStream m)
161foldr step acc m = go (toStream m)
@@ -167,7 +168,7 @@ foldr step acc m = go (toStream m)
168-- | Right fold with a monadic step function. See 'toList' for an example use.
169-- | Right fold with a monadic step function. See 'toList' for an example use.
169{-# INLINE foldrM #-}
170{-# INLINE foldrM #-}
170−foldrM :: (Streaming t, Monad m) => (a -> b -> m b) -> b -> t m a -> m b
171+foldrM :: (IsStream t, Monad m) => (a -> b -> m b) -> b -> t m a -> m b
171foldrM step acc m = go (toStream m)
172foldrM step acc m = go (toStream m)
@@ -178,7 +179,7 @@ foldrM step acc m = go (toStream m)
179-- | Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.
180-- | Scan left. A strict left fold which accumulates the result of its reduction steps inside a stream, from left.
180{-# INLINE scan #-}
181{-# INLINE scan #-}
181−scan :: Streaming t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
182+scan :: IsStream t => (x -> a -> x) -> x -> (x -> b) -> t m a -> t m b
182scan step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
183scan step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
184 go m1 !acc = Stream $ \_ stp yld ->
185 go m1 !acc = Stream $ \_ stp yld ->
@@ -192,7 +193,7 @@ scan step begin done m = cons (done begin) $ fromStream $ go (toStream m) begin
192-- | Strict left fold. This is typed to work with the foldl package. To use
193-- | Strict left fold. This is typed to work with the foldl package. To use
193-- it normally just pass 'id' as the third argument.
194-- it normally just pass 'id' as the third argument.
194{-# INLINE foldl #-}
195{-# INLINE foldl #-}
195−foldl :: (Streaming t, Monad m)
196+foldl :: (IsStream t, Monad m)
196 => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
197 => (x -> a -> x) -> x -> (x -> b) -> t m a -> m b
197foldl step begin done m = get $ go (toStream m) begin
198foldl step begin done m = get $ go (toStream m) begin
@@ -218,7 +219,7 @@ foldl step begin done m = get $ go (toStream m) begin
218-- XXX replace the recursive "go" with explicit continuations.
219-- XXX replace the recursive "go" with explicit continuations.
219-- | Strict left fold, with monadic step function. This is typed to work
220-- | Strict left fold, with monadic step function. This is typed to work
220-- with the foldl package. To use directly pass 'id' as the third argument.
221-- with the foldl package. To use directly pass 'id' as the third argument.
221−foldlM :: (Streaming t, Monad m)
222+foldlM :: (IsStream t, Monad m)
222 => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
223 => (x -> a -> m x) -> m x -> (x -> m b) -> t m a -> m b
223foldlM step begin done m = go begin (toStream m)
224foldlM step begin done m = go begin (toStream m)
@@ -231,7 +232,7 @@ foldlM step begin done m = go begin (toStream m)
231-- | Decompose a stream into its head and tail. If the stream is empty, returns
232-- | Decompose a stream into its head and tail. If the stream is empty, returns
232-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
233-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
233-- the head of the stream and 'ma' its tail.
234-- the head of the stream and 'ma' its tail.
234−uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a))
235+uncons :: (IsStream t, Monad m) => t m a -> m (Maybe (a, t m a))
235uncons m =
236uncons m =
236 let stop = return Nothing
237 let stop = return Nothing
237 yield a Nothing = return (Just (a, nil))
238 yield a Nothing = return (Just (a, nil))
@@ -239,7 +240,7 @@ uncons m =
239 in (runStream (toStream m)) Nothing stop yield
240 in (runStream (toStream m)) Nothing stop yield
241-- | Write a stream of Strings to an IO Handle.
242-- | Write a stream of Strings to an IO Handle.
242−toHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String -> m ()
243+toHandle :: (IsStream t, MonadIO m) => IO.Handle -> t m String -> m ()
243toHandle h m = go (toStream m)
244toHandle h m = go (toStream m)
@@ -254,12 +255,12 @@ toHandle h m = go (toStream m)
255-- | Convert a stream into a list in the underlying monad.
256-- | Convert a stream into a list in the underlying monad.
256{-# INLINABLE toList #-}
257{-# INLINABLE toList #-}
257−toList :: (Streaming t, Monad m) => t m a -> m [a]
258+toList :: (IsStream t, Monad m) => t m a -> m [a]
258toList = foldrM (\a xs -> return (a : xs)) []
259toList = foldrM (\a xs -> return (a : xs)) []
260-- | Take first 'n' elements from the stream and discard the rest.
261-- | Take first 'n' elements from the stream and discard the rest.
261{-# INLINE take #-}
262{-# INLINE take #-}
262−take :: Streaming t => Int -> t m a -> t m a
263+take :: IsStream t => Int -> t m a -> t m a
263take n m = fromStream $ go n (toStream m)
264take n m = fromStream $ go n (toStream m)
265 go n1 m1 = Stream $ \ctx stp yld ->
266 go n1 m1 = Stream $ \ctx stp yld ->
@@ -269,7 +270,7 @@ take n m = fromStream $ go n (toStream m)
270-- | Include only those elements that pass a predicate.
271-- | Include only those elements that pass a predicate.
271{-# INLINE filter #-}
272{-# INLINE filter #-}
272−filter :: Streaming t => (a -> Bool) -> t m a -> t m a
273+filter :: IsStream t => (a -> Bool) -> t m a -> t m a
273filter p m = fromStream $ go (toStream m)
274filter p m = fromStream $ go (toStream m)
275 go m1 = Stream $ \ctx stp yld ->
276 go m1 = Stream $ \ctx stp yld ->
@@ -281,7 +282,7 @@ filter p m = fromStream $ go (toStream m)
282-- | End the stream as soon as the predicate fails on an element.
283-- | End the stream as soon as the predicate fails on an element.
283{-# INLINE takeWhile #-}
284{-# INLINE takeWhile #-}
284−takeWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
285+takeWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
285takeWhile p m = fromStream $ go (toStream m)
286takeWhile p m = fromStream $ go (toStream m)
287 go m1 = Stream $ \ctx stp yld ->
288 go m1 = Stream $ \ctx stp yld ->
@@ -292,7 +293,7 @@ takeWhile p m = fromStream $ go (toStream m)
292 in (runStream m1) ctx stp yield
293 in (runStream m1) ctx stp yield
294-- | Discard first 'n' elements from the stream and take the rest.
295-- | Discard first 'n' elements from the stream and take the rest.
295−drop :: Streaming t => Int -> t m a -> t m a
296+drop :: IsStream t => Int -> t m a -> t m a
296drop n m = fromStream $ go n (toStream m)
297drop n m = fromStream $ go n (toStream m)
298 go n1 m1 = Stream $ \ctx stp yld ->
299 go n1 m1 = Stream $ \ctx stp yld ->
@@ -306,7 +307,7 @@ drop n m = fromStream $ go n (toStream m)
306-- | Drop elements in the stream as long as the predicate succeeds and then
307-- | Drop elements in the stream as long as the predicate succeeds and then
307-- take the rest of the stream.
308-- take the rest of the stream.
308{-# INLINE dropWhile #-}
309{-# INLINE dropWhile #-}
309−dropWhile :: Streaming t => (a -> Bool) -> t m a -> t m a
310+dropWhile :: IsStream t => (a -> Bool) -> t m a -> t m a
310dropWhile p m = fromStream $ go (toStream m)
311dropWhile p m = fromStream $ go (toStream m)
312 go m1 = Stream $ \ctx stp yld ->
313 go m1 = Stream $ \ctx stp yld ->
@@ -317,7 +318,7 @@ dropWhile p m = fromStream $ go (toStream m)
317 in (runStream m1) ctx stp yield
318 in (runStream m1) ctx stp yield
319-- | Determine whether all elements of a stream satisfy a predicate.
320-- | Determine whether all elements of a stream satisfy a predicate.
320−all :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool
321+all :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
321all p m = go (toStream m)
322all p m = go (toStream m)
@@ -328,7 +329,7 @@ all p m = go (toStream m)
328 in (runStream m1) Nothing (return True) yield
329 in (runStream m1) Nothing (return True) yield
330-- | Determine whether any of the elements of a stream satisfy a predicate.
331-- | Determine whether any of the elements of a stream satisfy a predicate.
331−any :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool
332+any :: (IsStream t, Monad m) => (a -> Bool) -> t m a -> m Bool
332any p m = go (toStream m)
333any p m = go (toStream m)
@@ -339,22 +340,22 @@ any p m = go (toStream m)
339 in (runStream m1) Nothing (return False) yield
340 in (runStream m1) Nothing (return False) yield
341-- | Determine the sum of all elements of a stream of numbers
342-- | Determine the sum of all elements of a stream of numbers
342−sum :: (Streaming t, Monad m, Num a) => t m a -> m a
343+sum :: (IsStream t, Monad m, Num a) => t m a -> m a
343sum = foldl (+) 0 id
344sum = foldl (+) 0 id
345-- | Determine the product of all elements of a stream of numbers
346-- | Determine the product of all elements of a stream of numbers
346−product :: (Streaming t, Monad m, Num a) => t m a -> m a
347+product :: (IsStream t, Monad m, Num a) => t m a -> m a
347product = foldl (*) 1 id
348product = foldl (*) 1 id
349-- | Extract the first element of the stream, if any.
350-- | Extract the first element of the stream, if any.
350−head :: (Streaming t, Monad m) => t m a -> m (Maybe a)
351+head :: (IsStream t, Monad m) => t m a -> m (Maybe a)
352 let stop = return Nothing
353 let stop = return Nothing
353 yield a _ = return (Just a)
354 yield a _ = return (Just a)
354 in (runStream (toStream m)) Nothing stop yield
355 in (runStream (toStream m)) Nothing stop yield
356-- | Extract all but the first element of the stream, if any.
357-- | Extract all but the first element of the stream, if any.
357−tail :: (Streaming t, Monad m) => t m a -> m (Maybe (t m a))
358+tail :: (IsStream t, Monad m) => t m a -> m (Maybe (t m a))
359 let stop = return Nothing
360 let stop = return Nothing
360 yield _ Nothing = return $ Just nil
361 yield _ Nothing = return $ Just nil
@@ -363,18 +364,18 @@ tail m =
364-- | Extract the last element of the stream, if any.
365-- | Extract the last element of the stream, if any.
365{-# INLINE last #-}
366{-# INLINE last #-}
366−last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
367+last :: (IsStream t, Monad m) => t m a -> m (Maybe a)
367last = foldl (\_ y -> Just y) Nothing id
368last = foldl (\_ y -> Just y) Nothing id
369-- | Determine whether the stream is empty.
370-- | Determine whether the stream is empty.
370−null :: (Streaming t, Monad m) => t m a -> m Bool
371+null :: (IsStream t, Monad m) => t m a -> m Bool
372 let stop = return True
373 let stop = return True
373 yield _ _ = return False
374 yield _ _ = return False
374 in (runStream (toStream m)) Nothing stop yield
375 in (runStream (toStream m)) Nothing stop yield
376-- | Determine whether an element is present in the stream.
377-- | Determine whether an element is present in the stream.
377−elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
378+elem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
378elem e m = go (toStream m)
379elem e m = go (toStream m)
@@ -384,7 +385,7 @@ elem e m = go (toStream m)
384 in (runStream m1) Nothing stop yield
385 in (runStream m1) Nothing stop yield
386-- | Determine whether an element is not present in the stream.
387-- | Determine whether an element is not present in the stream.
387−notElem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
388+notElem :: (IsStream t, Monad m, Eq a) => a -> t m a -> m Bool
388notElem e m = go (toStream m)
389notElem e m = go (toStream m)
@@ -394,12 +395,12 @@ notElem e m = go (toStream m)
394 in (runStream m1) Nothing stop yield
395 in (runStream m1) Nothing stop yield
396-- | Determine the length of the stream.
397-- | Determine the length of the stream.
397−length :: (Streaming t, Monad m) => t m a -> m Int
398+length :: (IsStream t, Monad m) => t m a -> m Int
398length = foldl (\n _ -> n + 1) 0 id
399length = foldl (\n _ -> n + 1) 0 id
400-- | Returns the elements of the stream in reverse order.
401-- | Returns the elements of the stream in reverse order.
401-- The stream must be finite.
402-- The stream must be finite.
402−reverse :: (Streaming t) => t m a -> t m a
403+reverse :: (IsStream t) => t m a -> t m a
403reverse m = fromStream $ go Nothing (toStream m)
404reverse m = fromStream $ go Nothing (toStream m)
405 go rev rest = Stream $ \svr stp yld ->
406 go rev rest = Stream $ \svr stp yld ->
@@ -412,7 +413,7 @@ reverse m = fromStream $ go Nothing (toStream m)
413-- XXX replace the recursive "go" with continuation
414-- XXX replace the recursive "go" with continuation
414-- | Determine the minimum element in a stream.
415-- | Determine the minimum element in a stream.
415−minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
416+minimum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
416minimum m = go Nothing (toStream m)
417minimum m = go Nothing (toStream m)
418 go r m1 =
419 go r m1 =
@@ -427,7 +428,7 @@ minimum m = go Nothing (toStream m)
428-- XXX replace the recursive "go" with continuation
429-- XXX replace the recursive "go" with continuation
429-- | Determine the maximum element in a stream.
430-- | Determine the maximum element in a stream.
430−maximum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
431+maximum :: (IsStream t, Monad m, Ord a) => t m a -> m (Maybe a)
431maximum m = go Nothing (toStream m)
432maximum m = go Nothing (toStream m)
433 go r m1 =
434 go r m1 =
@@ -449,7 +450,7 @@ maximum m = go Nothing (toStream m)
449-- | Replace each element of the stream with the result of a monadic action
450-- | Replace each element of the stream with the result of a monadic action
450-- applied on the element.
451-- applied on the element.
451{-# INLINE mapM #-}
452{-# INLINE mapM #-}
452−mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
453+mapM :: (IsStream t, Monad m) => (a -> m b) -> t m a -> t m b
453mapM f m = fromStream $ go (toStream m)
454mapM f m = fromStream $ go (toStream m)
455 go m1 = Stream $ \_ stp yld ->
456 go m1 = Stream $ \_ stp yld ->
@@ -460,7 +461,7 @@ mapM f m = fromStream $ go (toStream m)
461-- | Apply a monadic action to each element of the stream and discard the
462-- | Apply a monadic action to each element of the stream and discard the
462-- output of the action.
463-- output of the action.
463−mapM_ :: (Streaming t, Monad m) => (a -> m b) -> t m a -> m ()
464+mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
464mapM_ f m = go (toStream m)
465mapM_ f m = go (toStream m)
@@ -471,7 +472,7 @@ mapM_ f m = go (toStream m)
472-- | Reduce a stream of monadic actions to a stream of the output of those
473-- | Reduce a stream of monadic actions to a stream of the output of those
473-- actions.
474-- actions.
474−sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
475+sequence :: (IsStream t, Monad m) => t m (m a) -> t m a
475sequence m = fromStream $ go (toStream m)
476sequence m = fromStream $ go (toStream m)
477 go m1 = Stream $ \_ stp yld ->
478 go m1 = Stream $ \_ stp yld ->
@@ -481,7 +482,7 @@ sequence m = fromStream $ go (toStream m)
481 in (runStream m1) Nothing stop yield
482 in (runStream m1) Nothing stop yield
483-- | Generate a stream by performing an action @n@ times.
484-- | Generate a stream by performing an action @n@ times.
484−replicateM :: (Streaming t, Monad m) => Int -> m a -> t m a
485+replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
485replicateM n m = fromStream $ go n
486replicateM n m = fromStream $ go n
487 go cnt = Stream $ \_ stp yld ->
488 go cnt = Stream $ \_ stp yld ->
@@ -494,7 +495,7 @@ replicateM n m = fromStream $ go n
494------------------------------------------------------------------------------
495------------------------------------------------------------------------------
496-- | Zip two streams serially using a monadic zipping function.
497-- | Zip two streams serially using a monadic zipping function.
497−zipWithM :: Streaming t => (a -> b -> t m c) -> t m a -> t m b -> t m c
498+zipWithM :: IsStream t => (a -> b -> t m c) -> t m a -> t m b -> t m c
498zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
499zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
500 go mx my = Stream $ \_ stp yld -> do
501 go mx my = Stream $ \_ stp yld -> do
@@ -514,7 +515,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
515-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
516-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
516-- concurrently) using a monadic zipping function.
517-- concurrently) using a monadic zipping function.
517−zipAsyncWithM :: (Streaming t, MonadAsync m)
518+zipAsyncWithM :: (IsStream t, MonadAsync m)
518 => (a -> b -> t m c) -> t m a -> t m b -> t m c
519 => (a -> b -> t m c) -> t m a -> t m b -> t m c
519zipAsyncWithM f m1 m2 = fromStream $ Stream $ \_ stp yld -> do
520zipAsyncWithM f m1 m2 = fromStream $ Stream $ \_ stp yld -> do
520 ma <- async m1
521 ma <- async m1