@@ -18,10 +18,10 @@ module Streamly.Internal.Data.Fold
1818 , Fold (..)
1919
2020 -- * Creating
21− , mkAccum
22− , mkAccum_
23− , mkAccumM
24− , mkAccumM_
21+ , mkFoldl
22+ , mkFoldlM
23+ , mkFoldr
24+ , mkFoldrM
2525 , mkFold
2626 , mkFold_
2727 , mkFoldM
@@ -100,7 +100,6 @@ module Streamly.Internal.Data.Fold
100100 , runStep
101101
102102 -- * Output Transformations
103− , rsequence
104103 , sequence
105104 , rmapM
106105 , mapM
@@ -218,7 +217,7 @@ module Streamly.Internal.Data.Fold
218217 )
219218where
220219
221−import Control.Monad (void, join)
220+import Control.Monad (void)
222221import Data.Bifunctor (first)
223222import Data.Functor.Identity (Identity(..))
224223import Data.Int (Int64)
@@ -281,30 +280,10 @@ generally = hoist (return . runIdentity)
281280-- Transformations on fold inputs
282281------------------------------------------------------------------------------
283282
284−-- | Flatten the monadic output of a fold to pure output.
285−--
286−-- @since 0.8.0
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'
290−
291− where
292−
293− eval res =
294− case res of
295− Partial x -> return $ Partial x
296− Done b -> Done <$> b
297−
298− initial1 = initial >>= eval
299−
300− step' s a = step s a >>= eval
301−
302− extract' = join . extract
303−
304283-- | Flatten the monadic output of a fold to pure output.
305284--
306285-- @since 0.7.0
307−{-# DEPRECATED sequence "Use rsequence instead" #-}
286+{-# DEPRECATED sequence "Use \"rmapM id\" instead" #-}
308287{-# INLINE sequence #-}
309288sequence :: Monad m => Fold m a (m b) -> Fold m a b
310289sequence = rmapM id
@@ -386,7 +365,7 @@ transform (Pipe pstep1 pstep2 pinitial) (Fold fstep finitial fextract) =
386365-- from the 'Foldable', the result is 'None' for empty containers.
387366{-# INLINABLE _Fold1 #-}
388367_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'
390369
391370 where
392371
@@ -440,7 +419,7 @@ last = _Fold1 (\_ x -> x)
440419-- @since 0.7.0
441420{-# INLINE genericLength #-}
442421genericLength :: (Monad m, Num b) => Fold m a b
443−genericLength = mkAccum_ (\n _ -> n + 1) 0
422+genericLength = mkFoldl (\n _ -> n + 1) 0
444423
445424-- | Determine the length of the input stream.
446425--
@@ -460,7 +439,7 @@ length = genericLength
460439-- @since 0.7.0
461440{-# INLINE sum #-}
462441sum :: (Monad m, Num a) => Fold m a a
463−sum = mkAccum_ (+) 0
442+sum = mkFoldl (+) 0
464443
465444-- | Determine the product of all elements of a stream of numbers. Returns
466445-- multiplicative identity (@1@) when the stream is empty. The fold terminates
@@ -552,7 +531,7 @@ minimum = _Fold1 min
552531-- @since 0.7.0
553532{-# INLINABLE mean #-}
554533mean :: (Monad m, Fractional a) => Fold m a a
555−mean = mkAccum step begin done
534+mean = fmap done $ mkFoldl step begin
556535
557536 where
558537
@@ -570,7 +549,7 @@ mean = mkAccum step begin done
570549-- @since 0.7.0
571550{-# INLINABLE variance #-}
572551variance :: (Monad m, Fractional a) => Fold m a a
573−variance = mkAccum step begin done
552+variance = fmap done $ mkFoldl step begin
574553
575554 where
576555
@@ -609,7 +588,7 @@ stdDev = sqrt <$> variance
609588-- /Pre-release/
610589{-# INLINABLE rollingHashWithSalt #-}
611590rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
612−rollingHashWithSalt = mkAccum_ step
591+rollingHashWithSalt = mkFoldl step
613592
614593 where
615594
@@ -653,7 +632,7 @@ rollingHashFirstN n = take n rollingHash
653632--
654633{-# INLINE sconcat #-}
655634sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
656−sconcat = mkAccum_ (<>)
635+sconcat = mkFoldl (<>)
657636
658637-- | Fold an input stream consisting of monoidal elements using 'mappend'
659638-- and 'mempty'.
@@ -698,7 +677,7 @@ foldMap f = map f mconcat
698677-- @since 0.7.0
699678{-# INLINABLE foldMapM #-}
700679foldMapM :: (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)
702681
703682 where
704683
@@ -724,7 +703,7 @@ foldMapM act = mkAccumM_ step (pure mempty)
724703-- xn : ... : x2 : x1 : []
725704{-# INLINABLE toListRev #-}
726705toListRev :: Monad m => Fold m a [a]
727−toListRev = mkAccum_ (flip (:)) []
706+toListRev = mkFoldl (flip (:)) []
728707
729708------------------------------------------------------------------------------
730709-- Partial Folds
@@ -1525,7 +1504,8 @@ demuxDefault = demuxDefaultWith id
15251504--
15261505{-# INLINE classifyWith #-}
15271506classifyWith :: (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
15291509
15301510 where
15311511
@@ -1812,4 +1792,4 @@ toStream = mkFoldr K.cons K.nil
18121792-- xn : ... : x2 : x1 : []
18131793{-# INLINABLE toStreamRev #-}
18141794toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1815−toStreamRev = mkAccum_ (flip K.cons) K.nil
1795+toStreamRev = mkFoldl (flip K.cons) K.nil