@@ -1,10 +1,15 @@
11{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE CPP #-}
23{-# LANGUAGE FlexibleContexts #-}
34{-# LANGUAGE FlexibleInstances #-}
45{-# LANGUAGE MultiParamTypeClasses #-}
56{-# LANGUAGE RankNTypes #-}
67{-# LANGUAGE UndecidableInstances #-} -- XXX
78
9+{-# OPTIONS_GHC -Wno-orphans #-}
10+
11+#include "Streams/inline.h"
12+
813-- |
914-- Module : Streamly.Prelude
1015-- Copyright : (c) 2017 Harendra Kumar
@@ -60,7 +65,9 @@ module Streamly.Prelude
6065 , repeatM
6166 , iterate
6267 , iterateM
63− , fromFoldable
68+ , fromList
69+ , fromListM
70+ , K.fromFoldable
6471 , fromFoldableM
6572
6673 -- * Deconstruction
@@ -123,7 +130,7 @@ module Streamly.Prelude
123130 , toHandle
124131
125132 -- * Deprecated
126− , once
133+ , K.once
127134 , each
128135 , scan
129136 , foldl
@@ -144,8 +151,9 @@ import Prelude hiding (filter, drop, dropWhile, take,
144151import qualified Prelude
145152import qualified System.IO as IO
146153
147−import Streamly.Streams.StreamK
154+import Streamly.Streams.StreamK (IsStream(..), cons, nil, (.:))
148155import qualified Streamly.Streams.StreamK as K
156+import qualified Streamly.Streams.StreamD as D
149157import Streamly.Streams.Zip
150158import Streamly.Streams.Serial
151159import Streamly.SVar (MonadAsync)
@@ -171,14 +179,11 @@ import Streamly.SVar (MonadAsync)
171179-- @
172180--
173181-- @since 0.1.0
174−{-# INLINE unfoldr #-}
175−unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
176−unfoldr step = fromStream . go
177− where
178− go s = K.Stream $ \_ stp _ yld ->
179− case step s of
180− Nothing -> stp
181− Just (a, b) -> yld a (go b)
182+{-# INLINE_EARLY unfoldr #-}
183+unfoldr :: (Monad m, IsStream t) => (b -> Maybe (a, b)) -> b -> t m a
184+unfoldr step seed = fromStream $ D.toStreamK (D.unfoldr step seed)
185+{-# RULES "unfoldr fallback to StreamK" [1]
186+ forall a b. D.toStreamK (D.unfoldr a b) = K.unfoldr a b #-}
182187
183188-- | Build a stream by unfolding a /monadic/ step function starting from a
184189-- seed. The step function returns the next element in the stream and the next
@@ -211,23 +216,35 @@ unfoldr step = fromStream . go
211216-- /Concurrent/
212217--
213218-- /Since: 0.1.0/
214−{-# INLINE unfoldrM #-}
219+{-# INLINE_EARLY unfoldrM #-}
215220unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
216−unfoldrM step = go
217− where
218− go s = fromStream $ K.Stream $ \svr stp sng yld -> do
219− mayb <- step s
220− case mayb of
221− Nothing -> stp
222− Just (a, b) ->
223− K.unStream (toStream (return a |: go b)) svr stp sng yld
224−
225−-- | Construct a stream from a 'Foldable' containing pure values.
226−--
227−-- @since 0.2.0
228−{-# INLINE fromFoldable #-}
229−fromFoldable :: (IsStream t, Foldable f) => f a -> t m a
230−fromFoldable = Prelude.foldr cons nil
221+unfoldrM = K.unfoldrM
222+
223+{-# RULES "unfoldrM serial" unfoldrM = unfoldrMSerial #-}
224+{-# INLINE_EARLY unfoldrMSerial #-}
225+unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
226+unfoldrMSerial step seed = fromStream $ D.toStreamK (D.unfoldrM step seed)
227+
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.
230+--
231+-- @since 0.4.0
232+{-# INLINE_EARLY fromList #-}
233+fromList :: (Monad m, IsStream t) => [a] -> t m a
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 #-}
237+
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
240+-- fuse the list.
241+--
242+-- @since 0.4.0
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 #-}
231248
232249-- | Construct a stream from a 'Foldable' containing monadic actions.
233250--
@@ -249,7 +266,7 @@ fromFoldableM = Prelude.foldr consM nil
249266{-# DEPRECATED each "Please use fromFoldable instead." #-}
250267{-# INLINE each #-}
251268each :: (IsStream t, Foldable f) => f a -> t m a
252−each = fromFoldable
269+each = K.fromFoldable
253270
254271-- | Generate a stream by performing a monadic action @n@ times.
255272--