@@ -1,10 +1,15 @@
1{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE RankNTypes #-}
6{-# LANGUAGE UndecidableInstances #-} -- XXX
7{-# LANGUAGE UndecidableInstances #-} -- XXX
9+{-# OPTIONS_GHC -Wno-orphans #-}
11+#include "Streams/inline.h"
9-- Module : Streamly.Prelude
14-- Module : Streamly.Prelude
10-- Copyright : (c) 2017 Harendra Kumar
15-- Copyright : (c) 2017 Harendra Kumar
@@ -60,7 +65,9 @@ module Streamly.Prelude
62 , iterateM
67 , iterateM
63− , fromFoldable
70+ , K.fromFoldable
64 , fromFoldableM
71 , fromFoldableM
66 -- * Deconstruction
73 -- * Deconstruction
@@ -123,7 +130,7 @@ module Streamly.Prelude
123 , toHandle
130 , toHandle
125 -- * Deprecated
132 -- * Deprecated
@@ -144,8 +151,9 @@ import Prelude hiding (filter, drop, dropWhile, take,
144import qualified Prelude
151import qualified Prelude
145import qualified System.IO as IO
152import qualified System.IO as IO
147−import Streamly.Streams.StreamK
154+import Streamly.Streams.StreamK (IsStream(..), cons, nil, (.:))
148import qualified Streamly.Streams.StreamK as K
155import qualified Streamly.Streams.StreamK as K
156+import qualified Streamly.Streams.StreamD as D
149import Streamly.Streams.Zip
157import Streamly.Streams.Zip
150import Streamly.Streams.Serial
158import Streamly.Streams.Serial
151import Streamly.SVar (MonadAsync)
159import Streamly.SVar (MonadAsync)
@@ -171,14 +179,11 @@ import Streamly.SVar (MonadAsync)
173-- @since 0.1.0
181-- @since 0.1.0
174−{-# INLINE unfoldr #-}
182+{-# INLINE_EARLY unfoldr #-}
175−unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
183+unfoldr :: (Monad m, IsStream t) => (b -> Maybe (a, b)) -> b -> t m a
176−unfoldr step = fromStream . go
184+unfoldr step seed = fromStream $ D.toStreamK (D.unfoldr step seed)
177− where
185+{-# RULES "unfoldr fallback to StreamK" [1]
178− go s = K.Stream $ \_ stp _ yld ->
186+ forall a b. D.toStreamK (D.unfoldr a b) = K.unfoldr a b #-}
181− Just (a, b) -> yld a (go b)
183-- | Build a stream by unfolding a /monadic/ step function starting from a
188-- | Build a stream by unfolding a /monadic/ step function starting from a
184-- seed. The step function returns the next element in the stream and the next
189-- seed. The step function returns the next element in the stream and the next
@@ -211,23 +216,35 @@ unfoldr step = fromStream . go
211-- /Concurrent/
216-- /Concurrent/
213-- /Since: 0.1.0/
218-- /Since: 0.1.0/
214−{-# INLINE unfoldrM #-}
219+{-# INLINE_EARLY unfoldrM #-}
215unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
220unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
216−unfoldrM step = go
221+unfoldrM = K.unfoldrM
218− go s = fromStream $ K.Stream $ \svr stp sng yld -> do
223+{-# RULES "unfoldrM serial" unfoldrM = unfoldrMSerial #-}
219− mayb <- step s
224+{-# INLINE_EARLY unfoldrMSerial #-}
220− case mayb of
225+unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
221− Nothing -> stp
226+unfoldrMSerial step seed = fromStream $ D.toStreamK (D.unfoldrM step seed)
223− K.unStream (toStream (return a |: go b)) svr stp sng yld
225−-- | Construct a stream from a 'Foldable' containing pure values.
228+-- | Construct a stream from a list containing pure values. This can be more
229+-- efficient than 'K.fromFoldable' for lists as it can fuse the list.
227−-- @since 0.2.0
231+-- @since 0.4.0
228−{-# INLINE fromFoldable #-}
232+{-# INLINE_EARLY fromList #-}
229−fromFoldable :: (IsStream t, Foldable f) => f a -> t m a
233+fromList :: (Monad m, IsStream t) => [a] -> t m a
230−fromFoldable = Prelude.foldr cons nil
234+fromList = fromStream . D.toStreamK . D.fromList
235+{-# RULES "fromList fallback to StreamK" [1]
236+ forall a. D.toStreamK (D.fromList a) = K.fromFoldable a #-}
238+-- | Construct a stream from a list containing monadic actions. This can be
239+-- more efficient than 'fromFoldableM' especially for serial streams as it can
243+{-# INLINE_EARLY fromListM #-}
244+fromListM :: (MonadAsync m, IsStream t) => [m a] -> t m a
245+fromListM = fromStream . D.toStreamK . D.fromListM
246+{-# RULES "fromListM fallback to StreamK" [1]
247+ forall a. D.toStreamK (D.fromListM a) = fromFoldableM a #-}
232-- | Construct a stream from a 'Foldable' containing monadic actions.
249-- | Construct a stream from a 'Foldable' containing monadic actions.
@@ -249,7 +266,7 @@ fromFoldableM = Prelude.foldr consM nil
249{-# DEPRECATED each "Please use fromFoldable instead." #-}
266{-# DEPRECATED each "Please use fromFoldable instead." #-}
250{-# INLINE each #-}
267{-# INLINE each #-}
251each :: (IsStream t, Foldable f) => f a -> t m a
268each :: (IsStream t, Foldable f) => f a -> t m a
252−each = fromFoldable
269+each = K.fromFoldable
254-- | Generate a stream by performing a monadic action @n@ times.
271-- | Generate a stream by performing a monadic action @n@ times.