@@ -19,11 +19,11 @@ module Streamly.Internal.Data.Fold
19 , Fold (..)
19 , Fold (..)
21 -- * Constructors
21 -- * Constructors
22− , mkFoldl
22+ , foldl'
23− , mkFoldlM
23+ , foldlM'
24− , mkFoldl1
24+ , foldl1
26− , mkFoldrM
26+ , foldrM
@@ -259,8 +259,8 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
259import qualified Prelude
259import qualified Prelude
261import Prelude hiding
261import Prelude hiding
262− ( filter, drop, dropWhile, take, takeWhile, zipWith
262+ ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
263− , foldl, map, mapM_, sequence, all, any, sum, product, elem
263+ , foldl, foldr, map, mapM_, sequence, all, any, sum, product, elem
264 , notElem, maximum, minimum, head, last, tail, length, null
264 , notElem, maximum, minimum, head, last, tail, length, null
265 , reverse, iterate, init, and, or, lookup, (!!)
265 , reverse, iterate, init, and, or, lookup, (!!)
266 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
266 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
@@ -410,7 +410,7 @@ drainBy2 f = Fold2 (const (void . f)) (\_ -> return ()) return
410-- @since 0.7.0
410-- @since 0.7.0
411{-# INLINABLE last #-}
411{-# INLINABLE last #-}
412last :: Monad m => Fold m a (Maybe a)
412last :: Monad m => Fold m a (Maybe a)
413−last = mkFoldl1 (\_ x -> x)
413+last = foldl1 (\_ x -> x)
415------------------------------------------------------------------------------
415------------------------------------------------------------------------------
416-- To Summary
416-- To Summary
@@ -423,7 +423,7 @@ last = mkFoldl1 (\_ x -> x)
423-- /Pre-release/
423-- /Pre-release/
424{-# INLINE genericLength #-}
424{-# INLINE genericLength #-}
425genericLength :: (Monad m, Num b) => Fold m a b
425genericLength :: (Monad m, Num b) => Fold m a b
426−genericLength = mkFoldl (\n _ -> n + 1) 0
426+genericLength = foldl' (\n _ -> n + 1) 0
428-- | Determine the length of the input stream.
428-- | Determine the length of the input stream.
@@ -443,7 +443,7 @@ length = genericLength
443-- @since 0.7.0
443-- @since 0.7.0
444{-# INLINE sum #-}
444{-# INLINE sum #-}
445sum :: (Monad m, Num a) => Fold m a a
445sum :: (Monad m, Num a) => Fold m a a
446−sum = mkFoldl (+) 0
446+sum = foldl' (+) 0
448-- | Determine the product of all elements of a stream of numbers. Returns
448-- | Determine the product of all elements of a stream of numbers. Returns
449-- multiplicative identity (@1@) when the stream is empty. The fold terminates
449-- multiplicative identity (@1@) when the stream is empty. The fold terminates
@@ -474,7 +474,7 @@ product = mkFold_ step (Partial 1)
474-- @since 0.7.0
474-- @since 0.7.0
475{-# INLINE maximumBy #-}
475{-# INLINE maximumBy #-}
476maximumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
476maximumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
477−maximumBy cmp = mkFoldl1 max'
477+maximumBy cmp = foldl1 max'
@@ -495,14 +495,14 @@ maximumBy cmp = mkFoldl1 max'
495-- @since 0.7.0
495-- @since 0.7.0
496{-# INLINE maximum #-}
496{-# INLINE maximum #-}
497maximum :: (Monad m, Ord a) => Fold m a (Maybe a)
497maximum :: (Monad m, Ord a) => Fold m a (Maybe a)
498−maximum = mkFoldl1 max
498+maximum = foldl1 max
500-- | Computes the minimum element with respect to the given comparison function
500-- | Computes the minimum element with respect to the given comparison function
502-- @since 0.7.0
502-- @since 0.7.0
503{-# INLINE minimumBy #-}
503{-# INLINE minimumBy #-}
504minimumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
504minimumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
505−minimumBy cmp = mkFoldl1 min'
505+minimumBy cmp = foldl1 min'
@@ -523,7 +523,7 @@ minimumBy cmp = mkFoldl1 min'
523-- @since 0.7.0
523-- @since 0.7.0
524{-# INLINE minimum #-}
524{-# INLINE minimum #-}
525minimum :: (Monad m, Ord a) => Fold m a (Maybe a)
525minimum :: (Monad m, Ord a) => Fold m a (Maybe a)
526−minimum = mkFoldl1 min
526+minimum = foldl1 min
528------------------------------------------------------------------------------
528------------------------------------------------------------------------------
529-- To Summary (Statistical)
529-- To Summary (Statistical)
@@ -535,7 +535,7 @@ minimum = mkFoldl1 min
535-- @since 0.7.0
535-- @since 0.7.0
536{-# INLINABLE mean #-}
536{-# INLINABLE mean #-}
537mean :: (Monad m, Fractional a) => Fold m a a
537mean :: (Monad m, Fractional a) => Fold m a a
538−mean = fmap done $ mkFoldl step begin
538+mean = fmap done $ foldl' step begin
@@ -553,7 +553,7 @@ mean = fmap done $ mkFoldl step begin
553-- @since 0.7.0
553-- @since 0.7.0
554{-# INLINABLE variance #-}
554{-# INLINABLE variance #-}
555variance :: (Monad m, Fractional a) => Fold m a a
555variance :: (Monad m, Fractional a) => Fold m a a
556−variance = fmap done $ mkFoldl step begin
556+variance = fmap done $ foldl' step begin
@@ -592,7 +592,7 @@ stdDev = sqrt <$> variance
592-- /Pre-release/
592-- /Pre-release/
593{-# INLINABLE rollingHashWithSalt #-}
593{-# INLINABLE rollingHashWithSalt #-}
594rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
594rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
595−rollingHashWithSalt = mkFoldl step
595+rollingHashWithSalt = foldl' step
@@ -634,13 +634,13 @@ rollingHashFirstN n = take n rollingHash
634-- Sum {getSum = 65}
634-- Sum {getSum = 65}
637−-- sconcat = Fold.mkFoldl (<>)
637+-- sconcat = Fold.foldl' (<>)
640-- @since 0.8.0
640-- @since 0.8.0
641{-# INLINE sconcat #-}
641{-# INLINE sconcat #-}
642sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
642sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
643−sconcat = mkFoldl (<>)
643+sconcat = foldl' (<>)
645-- | Fold an input stream consisting of monoidal elements using 'mappend'
645-- | Fold an input stream consisting of monoidal elements using 'mappend'
646-- and 'mempty'.
646-- and 'mempty'.
@@ -690,7 +690,7 @@ foldMap f = lmap f mconcat
690-- @since 0.7.0
690-- @since 0.7.0
691{-# INLINABLE foldMapM #-}
691{-# INLINABLE foldMapM #-}
692foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
692foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
693−foldMapM act = mkFoldlM step (pure mempty)
693+foldMapM act = foldlM' step (pure mempty)
@@ -708,7 +708,7 @@ foldMapM act = mkFoldlM step (pure mempty)
709-- | Buffers the input stream to a list in the reverse order of the input.
709-- | Buffers the input stream to a list in the reverse order of the input.
711−-- > toListRev = Fold.mkFoldl (flip (:)) []
711+-- > toListRev = Fold.foldl' (flip (:)) []
713-- /Warning!/ working on large lists accumulated as buffers in memory could be
713-- /Warning!/ working on large lists accumulated as buffers in memory could be
714-- very inefficient, consider using "Streamly.Array" instead.
714-- very inefficient, consider using "Streamly.Array" instead.
@@ -718,7 +718,7 @@ foldMapM act = mkFoldlM step (pure mempty)
718-- xn : ... : x2 : x1 : []
718-- xn : ... : x2 : x1 : []
719{-# INLINABLE toListRev #-}
719{-# INLINABLE toListRev #-}
720toListRev :: Monad m => Fold m a [a]
720toListRev :: Monad m => Fold m a [a]
721−toListRev = mkFoldl (flip (:)) []
721+toListRev = foldl' (flip (:)) []
723------------------------------------------------------------------------------
723------------------------------------------------------------------------------
724-- Partial Folds
724-- Partial Folds
@@ -1101,7 +1101,7 @@ tee = teeWith (,)
1101-- @since 0.7.0
1101-- @since 0.7.0
1102{-# INLINE distribute #-}
1102{-# INLINE distribute #-}
1103distribute :: Monad m => [Fold m a b] -> Fold m a [b]
1103distribute :: Monad m => [Fold m a b] -> Fold m a [b]
1104−distribute = foldr (teeWith (:)) (yield [])
1104+distribute = Prelude.foldr (teeWith (:)) (yield [])
1106------------------------------------------------------------------------------
1106------------------------------------------------------------------------------
1107-- Partitioning
1107-- Partitioning
@@ -1495,7 +1495,7 @@ demuxDefault = demuxDefaultWith id
1495{-# INLINE classifyWith #-}
1495{-# INLINE classifyWith #-}
1496classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1496classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1497classifyWith f (Fold step1 initial1 extract1) =
1497classifyWith f (Fold step1 initial1 extract1) =
1498− rmapM extract $ mkFoldlM step initial
1498+ rmapM extract $ foldlM' step initial
@@ -1764,12 +1764,12 @@ chunksBetween _low _high _f1 _f2 = undefined
1764-- /Warning!/ working on large streams accumulated as buffers in memory could
1764-- /Warning!/ working on large streams accumulated as buffers in memory could
1765-- be very inefficient, consider using "Streamly.Data.Array" instead.
1765-- be very inefficient, consider using "Streamly.Data.Array" instead.
1767−-- > toStream = mkFoldr K.cons K.nil
1767+-- > toStream = foldr K.cons K.nil
1769-- /Pre-release/
1769-- /Pre-release/
1770{-# INLINE toStream #-}
1770{-# INLINE toStream #-}
1771toStream :: Monad m => Fold m a (SerialT Identity a)
1771toStream :: Monad m => Fold m a (SerialT Identity a)
1772−toStream = mkFoldr K.cons K.nil
1772+toStream = foldr K.cons K.nil
1774-- This is more efficient than 'toStream'. toStream is exactly the same as
1774-- This is more efficient than 'toStream'. toStream is exactly the same as
1775-- reversing the stream after toStreamRev.
1775-- reversing the stream after toStreamRev.
@@ -1777,7 +1777,7 @@ toStream = mkFoldr K.cons K.nil
1777-- | Buffers the input stream to a pure stream in the reverse order of the
1777-- | Buffers the input stream to a pure stream in the reverse order of the
1778-- input.
1778-- input.
1780−-- > toStreamRev = mkFoldl (flip K.cons) K.nil
1780+-- > toStreamRev = foldl' (flip K.cons) K.nil
1782-- /Warning!/ working on large streams accumulated as buffers in memory could
1782-- /Warning!/ working on large streams accumulated as buffers in memory could
1783-- be very inefficient, consider using "Streamly.Data.Array" instead.
1783-- be very inefficient, consider using "Streamly.Data.Array" instead.
@@ -1787,4 +1787,4 @@ toStream = mkFoldr K.cons K.nil
1787-- xn : ... : x2 : x1 : []
1787-- xn : ... : x2 : x1 : []
1788{-# INLINABLE toStreamRev #-}
1788{-# INLINABLE toStreamRev #-}
1789toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1789toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1790−toStreamRev = mkFoldl (flip K.cons) K.nil
1790+toStreamRev = foldl' (flip K.cons) K.nil