Move the unfold combinator to Stream prelude module

This commit is contained in:
Harendra Kumar 2019-09-29 12:51:01 +05:30
parent 4810c8eef6
commit a1b976281a
3 changed files with 26 additions and 28 deletions

View File

@ -56,13 +56,7 @@
module Streamly.Internal.Data.Unfold
(
-- * Unfold Type
-- |
-- A 'Fold' can be run over a seed using the 'unfold' combinator.
--
-- >>> unfold UF.replicateM (putStrLn "hello") 10
Unfold
, unfold
-- * Operations on Input
, close
@ -113,28 +107,6 @@ import Streamly.Internal.Data.Unfold.Types (Unfold(..))
import Streamly.Internal.Data.Fold.Types (Fold(..))
import Streamly.Internal.Data.SVar (defState)
-------------------------------------------------------------------------------
-- Running unfolds
-------------------------------------------------------------------------------
-- XXX flip the first two arguments to make it similar to unfold i.e. returning
-- a -> Stream m b?
--
-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
--
{-# INLINE_NORMAL unfold #-}
unfold :: Monad m => a -> Unfold m a b -> Stream m b
unfold seed (Unfold ustep inject) = Stream step Nothing
where
{-# INLINE_LATE step #-}
step _ Nothing = inject seed >>= return . Skip . Just
step _ (Just st) = do
r <- ustep st
return $ case r of
Yield x s -> Yield x (Just s)
Skip s -> Skip (Just s)
Stop -> Stop
-------------------------------------------------------------------------------
-- Input operations
-------------------------------------------------------------------------------

View File

@ -45,6 +45,7 @@ module Streamly.Internal.Prelude
-- ** From Generators
, unfoldr
, unfoldrM
, unfold
, iterate
, iterateM
, fromIndices
@ -537,6 +538,15 @@ unfoldrM = K.unfoldrM
unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
unfoldrMSerial step seed = fromStreamS (S.unfoldrM step seed)
-- | Convert an 'Unfold' into a stream by supplying it an input seed.
--
-- >>> unfold UF.replicateM 10 (putStrLn "hello")
--
-- /Internal/
{-# INLINE unfold #-}
unfold :: (IsStream t, Monad m) => Unfold m a b -> a -> t m b
unfold unf x = fromStreamD $ D.unfold unf x
------------------------------------------------------------------------------
-- Specialized Generation
------------------------------------------------------------------------------

View File

@ -59,6 +59,7 @@ module Streamly.Streams.StreamD
-- ** Unfolds
, unfoldr
, unfoldrM
, unfold
-- ** Specialized Generation
-- | Generate a monadic stream from a seed.
@ -392,6 +393,21 @@ unfoldrM next state = Stream step state
unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
unfoldr f = unfoldrM (return . f)
-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
--
{-# INLINE_NORMAL unfold #-}
unfold :: Monad m => Unfold m a b -> a -> Stream m b
unfold (Unfold ustep inject) seed = Stream step Nothing
where
{-# INLINE_LATE step #-}
step _ Nothing = inject seed >>= return . Skip . Just
step _ (Just st) = do
r <- ustep st
return $ case r of
Yield x s -> Yield x (Just s)
Skip s -> Skip (Just s)
Stop -> Stop
------------------------------------------------------------------------------
-- Specialized Generation
------------------------------------------------------------------------------