@@ -18,10 +18,10 @@ module Streamly.Internal.Data.Fold
18 , Fold (..)
18 , Fold (..)
20 -- * Creating
20 -- * Creating
21− , mkAccum
21+ , mkFoldl
22− , mkAccum_
22+ , mkFoldlM
23− , mkAccumM
23+ , mkFoldr
24− , mkAccumM_
24+ , mkFoldrM
@@ -100,7 +100,6 @@ module Streamly.Internal.Data.Fold
100 , runStep
100 , runStep
102 -- * Output Transformations
102 -- * Output Transformations
104 , sequence
103 , sequence
@@ -218,7 +217,7 @@ module Streamly.Internal.Data.Fold
221−import Control.Monad (void, join)
220+import Control.Monad (void)
222import Data.Bifunctor (first)
221import Data.Bifunctor (first)
223import Data.Functor.Identity (Identity(..))
222import Data.Functor.Identity (Identity(..))
224import Data.Int (Int64)
223import Data.Int (Int64)
@@ -281,30 +280,10 @@ generally = hoist (return . runIdentity)
281-- Transformations on fold inputs
280-- Transformations on fold inputs
282------------------------------------------------------------------------------
281------------------------------------------------------------------------------
284−-- | Flatten the monadic output of a fold to pure output.
287−{-# INLINE rsequence #-}
288−rsequence :: Monad m => Fold m a (m b) -> Fold m a b
289−rsequence (Fold step initial extract) = Fold step' initial1 extract'
295− Partial x -> return $ Partial x
296− Done b -> Done <$> b
298− initial1 = initial >>= eval
300− step' s a = step s a >>= eval
302− extract' = join . extract
304-- | Flatten the monadic output of a fold to pure output.
283-- | Flatten the monadic output of a fold to pure output.
306-- @since 0.7.0
285-- @since 0.7.0
307−{-# DEPRECATED sequence "Use rsequence instead" #-}
286+{-# DEPRECATED sequence "Use \"rmapM id\" instead" #-}
308{-# INLINE sequence #-}
287{-# INLINE sequence #-}
309sequence :: Monad m => Fold m a (m b) -> Fold m a b
288sequence :: Monad m => Fold m a (m b) -> Fold m a b
310sequence = rmapM id
289sequence = rmapM id
@@ -386,7 +365,7 @@ transform (Pipe pstep1 pstep2 pinitial) (Fold fstep finitial fextract) =
386-- from the 'Foldable', the result is 'None' for empty containers.
365-- from the 'Foldable', the result is 'None' for empty containers.
387{-# INLINABLE _Fold1 #-}
366{-# INLINABLE _Fold1 #-}
388_Fold1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
367_Fold1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
389−_Fold1 step = mkAccum step_ Nothing' toMaybe
368+_Fold1 step = fmap toMaybe $ mkFoldl step_ Nothing'
@@ -440,7 +419,7 @@ last = _Fold1 (\_ x -> x)
440-- @since 0.7.0
419-- @since 0.7.0
441{-# INLINE genericLength #-}
420{-# INLINE genericLength #-}
442genericLength :: (Monad m, Num b) => Fold m a b
421genericLength :: (Monad m, Num b) => Fold m a b
443−genericLength = mkAccum_ (\n _ -> n + 1) 0
422+genericLength = mkFoldl (\n _ -> n + 1) 0
445-- | Determine the length of the input stream.
424-- | Determine the length of the input stream.
@@ -460,7 +439,7 @@ length = genericLength
460-- @since 0.7.0
439-- @since 0.7.0
461{-# INLINE sum #-}
440{-# INLINE sum #-}
462sum :: (Monad m, Num a) => Fold m a a
441sum :: (Monad m, Num a) => Fold m a a
463−sum = mkAccum_ (+) 0
442+sum = mkFoldl (+) 0
465-- | Determine the product of all elements of a stream of numbers. Returns
444-- | Determine the product of all elements of a stream of numbers. Returns
466-- multiplicative identity (@1@) when the stream is empty. The fold terminates
445-- multiplicative identity (@1@) when the stream is empty. The fold terminates
@@ -552,7 +531,7 @@ minimum = _Fold1 min
552-- @since 0.7.0
531-- @since 0.7.0
553{-# INLINABLE mean #-}
532{-# INLINABLE mean #-}
554mean :: (Monad m, Fractional a) => Fold m a a
533mean :: (Monad m, Fractional a) => Fold m a a
555−mean = mkAccum step begin done
534+mean = fmap done $ mkFoldl step begin
@@ -570,7 +549,7 @@ mean = mkAccum step begin done
570-- @since 0.7.0
549-- @since 0.7.0
571{-# INLINABLE variance #-}
550{-# INLINABLE variance #-}
572variance :: (Monad m, Fractional a) => Fold m a a
551variance :: (Monad m, Fractional a) => Fold m a a
573−variance = mkAccum step begin done
552+variance = fmap done $ mkFoldl step begin
@@ -609,7 +588,7 @@ stdDev = sqrt <$> variance
609-- /Pre-release/
588-- /Pre-release/
610{-# INLINABLE rollingHashWithSalt #-}
589{-# INLINABLE rollingHashWithSalt #-}
611rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
590rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
612−rollingHashWithSalt = mkAccum_ step
591+rollingHashWithSalt = mkFoldl step
@@ -653,7 +632,7 @@ rollingHashFirstN n = take n rollingHash
654{-# INLINE sconcat #-}
633{-# INLINE sconcat #-}
655sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
634sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
656−sconcat = mkAccum_ (<>)
635+sconcat = mkFoldl (<>)
658-- | Fold an input stream consisting of monoidal elements using 'mappend'
637-- | Fold an input stream consisting of monoidal elements using 'mappend'
659-- and 'mempty'.
638-- and 'mempty'.
@@ -698,7 +677,7 @@ foldMap f = map f mconcat
698-- @since 0.7.0
677-- @since 0.7.0
699{-# INLINABLE foldMapM #-}
678{-# INLINABLE foldMapM #-}
700foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
679foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
701−foldMapM act = mkAccumM_ step (pure mempty)
680+foldMapM act = mkFoldlM step (pure mempty)
@@ -724,7 +703,7 @@ foldMapM act = mkAccumM_ step (pure mempty)
724-- xn : ... : x2 : x1 : []
703-- xn : ... : x2 : x1 : []
725{-# INLINABLE toListRev #-}
704{-# INLINABLE toListRev #-}
726toListRev :: Monad m => Fold m a [a]
705toListRev :: Monad m => Fold m a [a]
727−toListRev = mkAccum_ (flip (:)) []
706+toListRev = mkFoldl (flip (:)) []
729------------------------------------------------------------------------------
708------------------------------------------------------------------------------
730-- Partial Folds
709-- Partial Folds
@@ -1525,7 +1504,8 @@ demuxDefault = demuxDefaultWith id
1526{-# INLINE classifyWith #-}
1505{-# INLINE classifyWith #-}
1527classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1506classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1528−classifyWith f (Fold step1 initial1 extract1) = mkAccumM step initial extract
1507+classifyWith f (Fold step1 initial1 extract1) =
1508+ rmapM extract $ mkFoldlM step initial
@@ -1812,4 +1792,4 @@ toStream = mkFoldr K.cons K.nil
1812-- xn : ... : x2 : x1 : []
1792-- xn : ... : x2 : x1 : []
1813{-# INLINABLE toStreamRev #-}
1793{-# INLINABLE toStreamRev #-}
1814toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1794toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1815−toStreamRev = mkAccum_ (flip K.cons) K.nil
1795+toStreamRev = mkFoldl (flip K.cons) K.nil