on master help sign in
Move the unfold combinator to Stream prelude module
a1b9762Harendra Kumar
src/Streamly/Internal/Data/Unfold.hs−28
@@ -56,13 +56,7 @@
56module Streamly.Internal.Data.Unfold
56module Streamly.Internal.Data.Unfold
57 (
57 (
58 -- * Unfold Type
58 -- * Unfold Type
59− -- |
60− -- A 'Fold' can be run over a seed using the 'unfold' combinator.
61− --
62− -- >>> unfold UF.replicateM (putStrLn "hello") 10
63−
64 Unfold
59 Unfold
65− , unfold
66
60
67 -- * Operations on Input
61 -- * Operations on Input
68 , close
62 , close
@@ -113,28 +107,6 @@ import Streamly.Internal.Data.Unfold.Types (Unfold(..))
113import Streamly.Internal.Data.Fold.Types (Fold(..))
107import Streamly.Internal.Data.Fold.Types (Fold(..))
114import Streamly.Internal.Data.SVar (defState)
108import Streamly.Internal.Data.SVar (defState)
115
109
116−-------------------------------------------------------------------------------
117−-- Running unfolds
118−-------------------------------------------------------------------------------
119−
120−-- XXX flip the first two arguments to make it similar to unfold i.e. returning
121−-- a -> Stream m b?
122−--
123−-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
124−--
125−{-# INLINE_NORMAL unfold #-}
126−unfold :: Monad m => a -> Unfold m a b -> Stream m b
127−unfold seed (Unfold ustep inject) = Stream step Nothing
128− where
129− {-# INLINE_LATE step #-}
130− step _ Nothing = inject seed >>= return . Skip . Just
131− step _ (Just st) = do
132− r <- ustep st
133− return $ case r of
134− Yield x s -> Yield x (Just s)
135− Skip s -> Skip (Just s)
136− Stop -> Stop
137−
138-------------------------------------------------------------------------------
110-------------------------------------------------------------------------------
139-- Input operations
111-- Input operations
140-------------------------------------------------------------------------------
112-------------------------------------------------------------------------------
src/Streamly/Internal/Prelude.hs+10
@@ -45,6 +45,7 @@ module Streamly.Internal.Prelude
45 -- ** From Generators
45 -- ** From Generators
46 , unfoldr
46 , unfoldr
47 , unfoldrM
47 , unfoldrM
48+ , unfold
48 , iterate
49 , iterate
49 , iterateM
50 , iterateM
50 , fromIndices
51 , fromIndices
@@ -537,6 +538,15 @@ unfoldrM = K.unfoldrM
537unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
538unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
538unfoldrMSerial step seed = fromStreamS (S.unfoldrM step seed)
539unfoldrMSerial step seed = fromStreamS (S.unfoldrM step seed)
539
540
541+-- | Convert an 'Unfold' into a stream by supplying it an input seed.
542+--
543+-- >>> unfold UF.replicateM 10 (putStrLn "hello")
544+--
545+-- /Internal/
546+{-# INLINE unfold #-}
547+unfold :: (IsStream t, Monad m) => Unfold m a b -> a -> t m b
548+unfold unf x = fromStreamD $ D.unfold unf x
549+
540------------------------------------------------------------------------------
550------------------------------------------------------------------------------
541-- Specialized Generation
551-- Specialized Generation
542------------------------------------------------------------------------------
552------------------------------------------------------------------------------
src/Streamly/Streams/StreamD.hs+16
@@ -59,6 +59,7 @@ module Streamly.Streams.StreamD
59 -- ** Unfolds
59 -- ** Unfolds
60 , unfoldr
60 , unfoldr
61 , unfoldrM
61 , unfoldrM
62+ , unfold
62
63
63 -- ** Specialized Generation
64 -- ** Specialized Generation
64 -- | Generate a monadic stream from a seed.
65 -- | Generate a monadic stream from a seed.
@@ -392,6 +393,21 @@ unfoldrM next state = Stream step state
392unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
393unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
393unfoldr f = unfoldrM (return . f)
394unfoldr f = unfoldrM (return . f)
394
395
396+-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
397+--
398+{-# INLINE_NORMAL unfold #-}
399+unfold :: Monad m => Unfold m a b -> a -> Stream m b
400+unfold (Unfold ustep inject) seed = Stream step Nothing
401+ where
402+ {-# INLINE_LATE step #-}
403+ step _ Nothing = inject seed >>= return . Skip . Just
404+ step _ (Just st) = do
405+ r <- ustep st
406+ return $ case r of
407+ Yield x s -> Yield x (Just s)
408+ Skip s -> Skip (Just s)
409+ Stop -> Stop
410+
395------------------------------------------------------------------------------
411------------------------------------------------------------------------------
396-- Specialized Generation
412-- Specialized Generation
397------------------------------------------------------------------------------
413------------------------------------------------------------------------------