on master help sign in
Move the unfold combinator to Stream prelude module
commita1b976281a98b1391cbdf0e257603965a75cecbe authorHarendra Kumar <harendra.kumar@gmail.com> dateSun 29 Sep 2019, 12:51 (7 years ago) parent4810c8e
3 files changed+26−28
src/Streamly/Internal/Data/Unfold.hs−28 src/Streamly/Internal/Prelude.hs+10 src/Streamly/Streams/StreamD.hs+16
src/Streamly/Internal/Data/Unfold.hs−28
@@ -56,13 +56,7 @@
5656module Streamly.Internal.Data.Unfold
5757 (
5858 -- * 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−
6459 Unfold
65− , unfold
6660
6761 -- * Operations on Input
6862 , close
@@ -113,28 +107,6 @@ import Streamly.Internal.Data.Unfold.Types (Unfold(..))
113107import Streamly.Internal.Data.Fold.Types (Fold(..))
114108import Streamly.Internal.Data.SVar (defState)
115109
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−
138110-------------------------------------------------------------------------------
139111-- Input operations
140112-------------------------------------------------------------------------------
src/Streamly/Internal/Prelude.hs+10
@@ -45,6 +45,7 @@ module Streamly.Internal.Prelude
4545 -- ** From Generators
4646 , unfoldr
4747 , unfoldrM
48+ , unfold
4849 , iterate
4950 , iterateM
5051 , fromIndices
@@ -537,6 +538,15 @@ unfoldrM = K.unfoldrM
537538unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
538539unfoldrMSerial step seed = fromStreamS (S.unfoldrM step seed)
539540
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+
540550------------------------------------------------------------------------------
541551-- Specialized Generation
542552------------------------------------------------------------------------------
src/Streamly/Streams/StreamD.hs+16
@@ -59,6 +59,7 @@ module Streamly.Streams.StreamD
5959 -- ** Unfolds
6060 , unfoldr
6161 , unfoldrM
62+ , unfold
6263
6364 -- ** Specialized Generation
6465 -- | Generate a monadic stream from a seed.
@@ -392,6 +393,21 @@ unfoldrM next state = Stream step state
392393unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
393394unfoldr f = unfoldrM (return . f)
394395
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+
395411------------------------------------------------------------------------------
396412-- Specialized Generation
397413------------------------------------------------------------------------------