@@ -55,6 +55,7 @@ module Streamly.Internal.Data.Unfold
59 -- ** Unfolds
60 -- ** Unfolds
60 , singleton
61 , singleton
@@ -63,16 +64,21 @@ module Streamly.Internal.Data.Unfold
67+import GHC.Types (SPEC(..))
66import Streamly.Streams.StreamD.Type (Stream(..), Step(..))
68import Streamly.Streams.StreamD.Type (Stream(..), Step(..))
67#if __GLASGOW_HASKELL__ < 800
69#if __GLASGOW_HASKELL__ < 800
68import Streamly.Streams.StreamD.Type (pattern Stream)
70import Streamly.Streams.StreamD.Type (pattern Stream)
70import Streamly.Internal.Data.Unfold.Types (Unfold(..))
72import Streamly.Internal.Data.Unfold.Types (Unfold(..))
73+import Streamly.Internal.Data.Fold.Types (Fold(..))
72-------------------------------------------------------------------------------
75-------------------------------------------------------------------------------
73-- Running unfolds
76-- Running unfolds
74-------------------------------------------------------------------------------
77-------------------------------------------------------------------------------
79+-- XXX flip the first two arguments to make it similar to unfold i.e. returning
76-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
82-- | Convert an 'Unfold' into a 'Stream' by supplying it a seed.
78{-# INLINE_NORMAL unfold #-}
84{-# INLINE_NORMAL unfold #-}
@@ -88,6 +94,28 @@ unfold seed (Unfold ustep inject) = Stream step Nothing
88 Skip s -> Skip (Just s)
94 Skip s -> Skip (Just s)
89 Stop -> Stop
95 Stop -> Stop
97+-- | Compose an 'Unfold' and a 'Fold'. Given an @Unfold m a b@ and a
98+-- @Fold m b c@, returns a monadic action @a -> m c@ representing the
99+-- application of the fold on the unfolded stream.
103+{-# INLINE_NORMAL fold #-}
104+fold :: Monad m => Unfold m a b -> Fold m b c -> a -> m c
105+fold (Unfold ustep inject) (Fold fstep initial extract) a =
106+ initial >>= \x -> inject a >>= go SPEC x
109+ {-# INLINE_LATE go #-}
110+ go !_ acc st = acc `seq` do
116+ Skip s -> go SPEC acc s
91-------------------------------------------------------------------------------
119-------------------------------------------------------------------------------
92-- Unfolds
120-- Unfolds
93-------------------------------------------------------------------------------
121-------------------------------------------------------------------------------