on master help sign in
Rename fold constructors in Data.Fold #1016
commitf203fa8c5d0271b511a40c22113bcf36d24b74a9 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateTue 6 Apr 2021, 15:08 (6 years ago) parent095409b
6 files changed+67−67
src/Streamly/Data/Fold.hs+3−3 src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+1−1 src/Streamly/Internal/Data/Array/Prim/MutTypesInclude.hs+2−2 src/Streamly/Internal/Data/Fold.hs+28−28 src/Streamly/Internal/Data/Fold/Type.hs+32−32 test/lib/Streamly/Test/Prelude/Common.hs+1−1
src/Streamly/Data/Fold.hs+3−3
@@ -119,9 +119,9 @@ module Streamly.Data.Fold
119119 Fold -- (..)
120120
121121 -- * Constructors
122− , mkFoldl
123− , mkFoldlM
124− , mkFoldr
122+ , foldl'
123+ , foldlM'
124+ , foldr
125125
126126 -- * Folds
127127 -- ** Accumulators
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+1−1
@@ -895,7 +895,7 @@ toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
895895 => Int -> Int -> Fold m a (Array a)
896896-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
897897toArrayMinChunk alignSize elemCount =
898− FL.rmapM extract $ FL.mkFoldlM step initial
898+ FL.rmapM extract $ FL.foldlM' step initial
899899
900900 where
901901
src/Streamly/Internal/Data/Array/Prim/MutTypesInclude.hs+2−2
@@ -137,7 +137,7 @@ shrinkArray (Array arr#) (I# n#) =
137137-- /Pre-release/
138138{-# INLINE_NORMAL write #-}
139139write :: (MonadIO m, Prim a) => Fold m a (Array a)
140−write = FL.rmapM extract $ FL.mkFoldlM step initial
140+write = FL.rmapM extract $ FL.foldlM' step initial
141141
142142 where
143143
@@ -193,7 +193,7 @@ data ArrayUnsafe a = ArrayUnsafe
193193-- /Pre-release/
194194{-# INLINE_NORMAL writeNUnsafe #-}
195195writeNUnsafe :: (MonadIO m, Prim a) => Int -> Fold m a (Array a)
196−writeNUnsafe n = FL.rmapM extract $ FL.mkFoldlM step initial
196+writeNUnsafe n = FL.rmapM extract $ FL.foldlM' step initial
197197
198198 where
199199
src/Streamly/Internal/Data/Fold.hs+28−28
@@ -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
src/Streamly/Internal/Data/Fold/Type.hs+32−32
@@ -196,11 +196,11 @@ module Streamly.Internal.Data.Fold.Type
196196 , Fold (..)
197197
198198 -- * Constructors
199− , mkFoldl
200− , mkFoldlM
201− , mkFoldl1
202− , mkFoldr
203− , mkFoldrM
199+ , foldl'
200+ , foldlM'
201+ , foldl1
202+ , foldr
203+ , foldrM
204204 , mkFold
205205 , mkFold_
206206 , mkFoldM
@@ -280,7 +280,7 @@ import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
280280import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
281281import Streamly.Internal.Data.SVar (MonadAsync)
282282
283−import Prelude hiding (concatMap, filter, map, take)
283+import Prelude hiding (concatMap, filter, foldl1, foldr, map, take)
284284
285285-- $setup
286286-- >>> :m
@@ -397,22 +397,22 @@ rmapM f (Fold step initial extract) = Fold step1 initial1 (extract >=> f)
397397-- the accumulator.
398398--
399399-- If your 'Fold' returns only 'Partial' (i.e. never returns a 'Done') then you
400−-- can use @mkFoldl*@ constructors.
400+-- can use @foldl'*@ constructors.
401401--
402402-- A fold with an extract function can be expressed using fmap:
403403--
404404-- @
405405-- mkfoldlx :: Monad m => (s -> a -> s) -> s -> (s -> b) -> Fold m a b
406−-- mkfoldlx step initial extract = fmap extract (mkFoldl step initial)
406+-- mkfoldlx step initial extract = fmap extract (foldl' step initial)
407407-- @
408408--
409409-- See also: "Streamly.Prelude.foldl'"
410410--
411411-- @since 0.8.0
412412--
413−{-# INLINE mkFoldl #-}
414−mkFoldl :: Monad m => (b -> a -> b) -> b -> Fold m a b
415−mkFoldl step initial =
413+{-# INLINE foldl' #-}
414+foldl' :: Monad m => (b -> a -> b) -> b -> Fold m a b
415+foldl' step initial =
416416 Fold
417417 (\s a -> return $ Partial $ step s a)
418418 (return (Partial initial))
@@ -425,16 +425,16 @@ mkFoldl step initial =
425425--
426426-- @
427427-- mkFoldlxM :: Functor m => (s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
428−-- mkFoldlxM step initial extract = rmapM extract (mkFoldlM step initial)
428+-- mkFoldlxM step initial extract = rmapM extract (foldlM' step initial)
429429-- @
430430--
431431-- See also: "Streamly.Prelude.foldlM'"
432432--
433433-- @since 0.8.0
434434--
435−{-# INLINE mkFoldlM #-}
436−mkFoldlM :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
437−mkFoldlM step initial =
435+{-# INLINE foldlM' #-}
436+foldlM' :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
437+foldlM' step initial =
438438 Fold (\s a -> Partial <$> step s a) (Partial <$> initial) return
439439
440440-- | Make a strict left fold, for non-empty streams, using first element as the
@@ -443,9 +443,9 @@ mkFoldlM step initial =
443443-- See also: "Streamly.Prelude.foldl1'"
444444--
445445-- /Pre-release/
446−{-# INLINE mkFoldl1 #-}
447−mkFoldl1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
448−mkFoldl1 step = fmap toMaybe $ mkFoldl step1 Nothing'
446+{-# INLINE foldl1 #-}
447+foldl1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
448+foldl1 step = fmap toMaybe $ foldl' step1 Nothing'
449449
450450 where
451451
@@ -463,31 +463,31 @@ mkFoldl1 step = fmap toMaybe $ mkFoldl step1 Nothing'
463463--
464464-- For example,
465465--
466−-- > toList = mkFoldr (:) []
466+-- > toList = foldr (:) []
467467--
468468-- See also: "Streamly.Prelude.foldr"
469469--
470470-- @since 0.8.0
471−{-# INLINE mkFoldr #-}
472−mkFoldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
473−mkFoldr g z = fmap ($ z) $ mkFoldl (\f x -> f . g x) id
471+{-# INLINE foldr #-}
472+foldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
473+foldr g z = fmap ($ z) $ foldl' (\f x -> f . g x) id
474474
475475-- XXX we have not seen any use of this yet, not releasing until we have a use
476476-- case.
477477--
478−-- | Like 'mkFoldr' but with a monadic step function.
478+-- | Like 'foldr' but with a monadic step function.
479479--
480480-- For example,
481481--
482−-- > toList = mkFoldrM (\a xs -> return $ a : xs) (return [])
482+-- > toList = foldrM (\a xs -> return $ a : xs) (return [])
483483--
484484-- See also: "Streamly.Prelude.foldrM"
485485--
486486-- /Pre-release/
487−{-# INLINE mkFoldrM #-}
488−mkFoldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
489−mkFoldrM g z =
490− rmapM (z >>=) $ mkFoldlM (\f x -> return $ g x >=> f) (return return)
487+{-# INLINE foldrM #-}
488+foldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
489+foldrM g z =
490+ rmapM (z >>=) $ foldlM' (\f x -> return $ g x >=> f) (return return)
491491
492492------------------------------------------------------------------------------
493493-- General fold constructors
@@ -498,7 +498,7 @@ mkFoldrM g z =
498498--
499499-- mkFold :: Monad m => (s -> a -> Step s b) -> Step s b -> Fold m a b
500500--
501−-- Then similar to mkFoldl and mkFoldr we can just fmap extract on it to extend
501+-- Then similar to foldl' and foldr we can just fmap extract on it to extend
502502-- it to the version where an 'extract' function is required. Or do we even
503503-- need that?
504504--
@@ -586,7 +586,7 @@ simplify (Fold2 step inject extract) c =
586586-- @since 0.7.0
587587{-# INLINABLE drain #-}
588588drain :: Monad m => Fold m a ()
589−drain = mkFoldl (\_ _ -> ()) ()
589+drain = foldl' (\_ _ -> ()) ()
590590
591591-- | Folds the input stream to a list.
592592--
@@ -594,12 +594,12 @@ drain = mkFoldl (\_ _ -> ()) ()
594594-- very inefficient, consider using "Streamly.Data.Array.Foreign"
595595-- instead.
596596--
597−-- > toList = mkFoldr (:) []
597+-- > toList = foldr (:) []
598598--
599599-- @since 0.7.0
600600{-# INLINABLE toList #-}
601601toList :: Monad m => Fold m a [a]
602−toList = mkFoldr (:) []
602+toList = foldr (:) []
603603
604604------------------------------------------------------------------------------
605605-- Instances
test/lib/Streamly/Test/Prelude/Common.hs+1−1
@@ -1165,7 +1165,7 @@ transformCombineOpsCommon constr desc eq t = do
11651165 monadicIO $ do
11661166 cref <- run $ newIORef 0
11671167 let fldstp _ e = modifyIORef' cref (e +)
1168− sumfoldinref = FL.mkFoldlM fldstp (return ())
1168+ sumfoldinref = FL.foldlM' fldstp (return ())
11691169 op = S.tap sumfoldinref . S.mapM (\x -> return (x+1))
11701170 listOp = fmap (+1)
11711171 stream <- run ((S.toList . t) $ op (constr a <> constr b))