@@ -19,11 +19,11 @@ module Streamly.Internal.Data.Fold
1919 , Fold (..)
2020
2121 -- * Constructors
22− , mkFoldl
23− , mkFoldlM
24− , mkFoldl1
25− , mkFoldr
26− , mkFoldrM
22+ , foldl'
23+ , foldlM'
24+ , foldl1
25+ , foldr
26+ , foldrM
2727 , mkFold
2828 , mkFold_
2929 , mkFoldM
@@ -259,8 +259,8 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
259259import qualified Prelude
260260
261261import Prelude hiding
262− ( filter, drop, dropWhile, take, takeWhile, zipWith
263− , foldl, map, mapM_, sequence, all, any, sum, product, elem
262+ ( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
263+ , foldl, foldr, map, mapM_, sequence, all, any, sum, product, elem
264264 , notElem, maximum, minimum, head, last, tail, length, null
265265 , reverse, iterate, init, and, or, lookup, (!!)
266266 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
@@ -410,7 +410,7 @@ drainBy2 f = Fold2 (const (void . f)) (\_ -> return ()) return
410410-- @since 0.7.0
411411{-# INLINABLE last #-}
412412last :: Monad m => Fold m a (Maybe a)
413−last = mkFoldl1 (\_ x -> x)
413+last = foldl1 (\_ x -> x)
414414
415415------------------------------------------------------------------------------
416416-- To Summary
@@ -423,7 +423,7 @@ last = mkFoldl1 (\_ x -> x)
423423-- /Pre-release/
424424{-# INLINE genericLength #-}
425425genericLength :: (Monad m, Num b) => Fold m a b
426−genericLength = mkFoldl (\n _ -> n + 1) 0
426+genericLength = foldl' (\n _ -> n + 1) 0
427427
428428-- | Determine the length of the input stream.
429429--
@@ -443,7 +443,7 @@ length = genericLength
443443-- @since 0.7.0
444444{-# INLINE sum #-}
445445sum :: (Monad m, Num a) => Fold m a a
446−sum = mkFoldl (+) 0
446+sum = foldl' (+) 0
447447
448448-- | Determine the product of all elements of a stream of numbers. Returns
449449-- multiplicative identity (@1@) when the stream is empty. The fold terminates
@@ -474,7 +474,7 @@ product = mkFold_ step (Partial 1)
474474-- @since 0.7.0
475475{-# INLINE maximumBy #-}
476476maximumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
477−maximumBy cmp = mkFoldl1 max'
477+maximumBy cmp = foldl1 max'
478478
479479 where
480480
@@ -495,14 +495,14 @@ maximumBy cmp = mkFoldl1 max'
495495-- @since 0.7.0
496496{-# INLINE maximum #-}
497497maximum :: (Monad m, Ord a) => Fold m a (Maybe a)
498−maximum = mkFoldl1 max
498+maximum = foldl1 max
499499
500500-- | Computes the minimum element with respect to the given comparison function
501501--
502502-- @since 0.7.0
503503{-# INLINE minimumBy #-}
504504minimumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
505−minimumBy cmp = mkFoldl1 min'
505+minimumBy cmp = foldl1 min'
506506
507507 where
508508
@@ -523,7 +523,7 @@ minimumBy cmp = mkFoldl1 min'
523523-- @since 0.7.0
524524{-# INLINE minimum #-}
525525minimum :: (Monad m, Ord a) => Fold m a (Maybe a)
526−minimum = mkFoldl1 min
526+minimum = foldl1 min
527527
528528------------------------------------------------------------------------------
529529-- To Summary (Statistical)
@@ -535,7 +535,7 @@ minimum = mkFoldl1 min
535535-- @since 0.7.0
536536{-# INLINABLE mean #-}
537537mean :: (Monad m, Fractional a) => Fold m a a
538−mean = fmap done $ mkFoldl step begin
538+mean = fmap done $ foldl' step begin
539539
540540 where
541541
@@ -553,7 +553,7 @@ mean = fmap done $ mkFoldl step begin
553553-- @since 0.7.0
554554{-# INLINABLE variance #-}
555555variance :: (Monad m, Fractional a) => Fold m a a
556−variance = fmap done $ mkFoldl step begin
556+variance = fmap done $ foldl' step begin
557557
558558 where
559559
@@ -592,7 +592,7 @@ stdDev = sqrt <$> variance
592592-- /Pre-release/
593593{-# INLINABLE rollingHashWithSalt #-}
594594rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
595−rollingHashWithSalt = mkFoldl step
595+rollingHashWithSalt = foldl' step
596596
597597 where
598598
@@ -634,13 +634,13 @@ rollingHashFirstN n = take n rollingHash
634634-- Sum {getSum = 65}
635635--
636636-- @
637−-- sconcat = Fold.mkFoldl (<>)
637+-- sconcat = Fold.foldl' (<>)
638638-- @
639639--
640640-- @since 0.8.0
641641{-# INLINE sconcat #-}
642642sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
643−sconcat = mkFoldl (<>)
643+sconcat = foldl' (<>)
644644
645645-- | Fold an input stream consisting of monoidal elements using 'mappend'
646646-- and 'mempty'.
@@ -690,7 +690,7 @@ foldMap f = lmap f mconcat
690690-- @since 0.7.0
691691{-# INLINABLE foldMapM #-}
692692foldMapM :: (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)
694694
695695 where
696696
@@ -708,7 +708,7 @@ foldMapM act = mkFoldlM step (pure mempty)
708708
709709-- | Buffers the input stream to a list in the reverse order of the input.
710710--
711−-- > toListRev = Fold.mkFoldl (flip (:)) []
711+-- > toListRev = Fold.foldl' (flip (:)) []
712712--
713713-- /Warning!/ working on large lists accumulated as buffers in memory could be
714714-- very inefficient, consider using "Streamly.Array" instead.
@@ -718,7 +718,7 @@ foldMapM act = mkFoldlM step (pure mempty)
718718-- xn : ... : x2 : x1 : []
719719{-# INLINABLE toListRev #-}
720720toListRev :: Monad m => Fold m a [a]
721−toListRev = mkFoldl (flip (:)) []
721+toListRev = foldl' (flip (:)) []
722722
723723------------------------------------------------------------------------------
724724-- Partial Folds
@@ -1101,7 +1101,7 @@ tee = teeWith (,)
11011101-- @since 0.7.0
11021102{-# INLINE distribute #-}
11031103distribute :: Monad m => [Fold m a b] -> Fold m a [b]
1104−distribute = foldr (teeWith (:)) (yield [])
1104+distribute = Prelude.foldr (teeWith (:)) (yield [])
11051105
11061106------------------------------------------------------------------------------
11071107-- Partitioning
@@ -1495,7 +1495,7 @@ demuxDefault = demuxDefaultWith id
14951495{-# INLINE classifyWith #-}
14961496classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
14971497classifyWith f (Fold step1 initial1 extract1) =
1498− rmapM extract $ mkFoldlM step initial
1498+ rmapM extract $ foldlM' step initial
14991499
15001500 where
15011501
@@ -1764,12 +1764,12 @@ chunksBetween _low _high _f1 _f2 = undefined
17641764-- /Warning!/ working on large streams accumulated as buffers in memory could
17651765-- be very inefficient, consider using "Streamly.Data.Array" instead.
17661766--
1767−-- > toStream = mkFoldr K.cons K.nil
1767+-- > toStream = foldr K.cons K.nil
17681768--
17691769-- /Pre-release/
17701770{-# INLINE toStream #-}
17711771toStream :: Monad m => Fold m a (SerialT Identity a)
1772−toStream = mkFoldr K.cons K.nil
1772+toStream = foldr K.cons K.nil
17731773
17741774-- This is more efficient than 'toStream'. toStream is exactly the same as
17751775-- reversing the stream after toStreamRev.
@@ -1777,7 +1777,7 @@ toStream = mkFoldr K.cons K.nil
17771777-- | Buffers the input stream to a pure stream in the reverse order of the
17781778-- input.
17791779--
1780−-- > toStreamRev = mkFoldl (flip K.cons) K.nil
1780+-- > toStreamRev = foldl' (flip K.cons) K.nil
17811781--
17821782-- /Warning!/ working on large streams accumulated as buffers in memory could
17831783-- be very inefficient, consider using "Streamly.Data.Array" instead.
@@ -1787,4 +1787,4 @@ toStream = mkFoldr K.cons K.nil
17871787-- xn : ... : x2 : x1 : []
17881788{-# INLINABLE toStreamRev #-}
17891789toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1790−toStreamRev = mkFoldl (flip K.cons) K.nil
1790+toStreamRev = foldl' (flip K.cons) K.nil