mirror of
https://github.com/ilyakooo0/streamly.git
synced 2024-09-11 08:25:40 +03:00
Rename fold constructors in Data.Fold #1016
This commit is contained in:
parent
095409b111
commit
f203fa8c5d
@ -119,9 +119,9 @@ module Streamly.Data.Fold
|
||||
Fold -- (..)
|
||||
|
||||
-- * Constructors
|
||||
, mkFoldl
|
||||
, mkFoldlM
|
||||
, mkFoldr
|
||||
, foldl'
|
||||
, foldlM'
|
||||
, foldr
|
||||
|
||||
-- * Folds
|
||||
-- ** Accumulators
|
||||
|
@ -895,7 +895,7 @@ toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
|
||||
=> Int -> Int -> Fold m a (Array a)
|
||||
-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
|
||||
toArrayMinChunk alignSize elemCount =
|
||||
FL.rmapM extract $ FL.mkFoldlM step initial
|
||||
FL.rmapM extract $ FL.foldlM' step initial
|
||||
|
||||
where
|
||||
|
||||
|
@ -137,7 +137,7 @@ shrinkArray (Array arr#) (I# n#) =
|
||||
-- /Pre-release/
|
||||
{-# INLINE_NORMAL write #-}
|
||||
write :: (MonadIO m, Prim a) => Fold m a (Array a)
|
||||
write = FL.rmapM extract $ FL.mkFoldlM step initial
|
||||
write = FL.rmapM extract $ FL.foldlM' step initial
|
||||
|
||||
where
|
||||
|
||||
@ -193,7 +193,7 @@ data ArrayUnsafe a = ArrayUnsafe
|
||||
-- /Pre-release/
|
||||
{-# INLINE_NORMAL writeNUnsafe #-}
|
||||
writeNUnsafe :: (MonadIO m, Prim a) => Int -> Fold m a (Array a)
|
||||
writeNUnsafe n = FL.rmapM extract $ FL.mkFoldlM step initial
|
||||
writeNUnsafe n = FL.rmapM extract $ FL.foldlM' step initial
|
||||
|
||||
where
|
||||
|
||||
|
@ -19,11 +19,11 @@ module Streamly.Internal.Data.Fold
|
||||
, Fold (..)
|
||||
|
||||
-- * Constructors
|
||||
, mkFoldl
|
||||
, mkFoldlM
|
||||
, mkFoldl1
|
||||
, mkFoldr
|
||||
, mkFoldrM
|
||||
, foldl'
|
||||
, foldlM'
|
||||
, foldl1
|
||||
, foldr
|
||||
, foldrM
|
||||
, mkFold
|
||||
, mkFold_
|
||||
, mkFoldM
|
||||
@ -259,8 +259,8 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
|
||||
import qualified Prelude
|
||||
|
||||
import Prelude hiding
|
||||
( filter, drop, dropWhile, take, takeWhile, zipWith
|
||||
, foldl, map, mapM_, sequence, all, any, sum, product, elem
|
||||
( filter, foldl1, drop, dropWhile, take, takeWhile, zipWith
|
||||
, foldl, foldr, map, mapM_, sequence, all, any, sum, product, elem
|
||||
, notElem, maximum, minimum, head, last, tail, length, null
|
||||
, reverse, iterate, init, and, or, lookup, (!!)
|
||||
, scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
|
||||
@ -410,7 +410,7 @@ drainBy2 f = Fold2 (const (void . f)) (\_ -> return ()) return
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE last #-}
|
||||
last :: Monad m => Fold m a (Maybe a)
|
||||
last = mkFoldl1 (\_ x -> x)
|
||||
last = foldl1 (\_ x -> x)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- To Summary
|
||||
@ -423,7 +423,7 @@ last = mkFoldl1 (\_ x -> x)
|
||||
-- /Pre-release/
|
||||
{-# INLINE genericLength #-}
|
||||
genericLength :: (Monad m, Num b) => Fold m a b
|
||||
genericLength = mkFoldl (\n _ -> n + 1) 0
|
||||
genericLength = foldl' (\n _ -> n + 1) 0
|
||||
|
||||
-- | Determine the length of the input stream.
|
||||
--
|
||||
@ -443,7 +443,7 @@ length = genericLength
|
||||
-- @since 0.7.0
|
||||
{-# INLINE sum #-}
|
||||
sum :: (Monad m, Num a) => Fold m a a
|
||||
sum = mkFoldl (+) 0
|
||||
sum = foldl' (+) 0
|
||||
|
||||
-- | Determine the product of all elements of a stream of numbers. Returns
|
||||
-- multiplicative identity (@1@) when the stream is empty. The fold terminates
|
||||
@ -474,7 +474,7 @@ product = mkFold_ step (Partial 1)
|
||||
-- @since 0.7.0
|
||||
{-# INLINE maximumBy #-}
|
||||
maximumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
|
||||
maximumBy cmp = mkFoldl1 max'
|
||||
maximumBy cmp = foldl1 max'
|
||||
|
||||
where
|
||||
|
||||
@ -495,14 +495,14 @@ maximumBy cmp = mkFoldl1 max'
|
||||
-- @since 0.7.0
|
||||
{-# INLINE maximum #-}
|
||||
maximum :: (Monad m, Ord a) => Fold m a (Maybe a)
|
||||
maximum = mkFoldl1 max
|
||||
maximum = foldl1 max
|
||||
|
||||
-- | Computes the minimum element with respect to the given comparison function
|
||||
--
|
||||
-- @since 0.7.0
|
||||
{-# INLINE minimumBy #-}
|
||||
minimumBy :: Monad m => (a -> a -> Ordering) -> Fold m a (Maybe a)
|
||||
minimumBy cmp = mkFoldl1 min'
|
||||
minimumBy cmp = foldl1 min'
|
||||
|
||||
where
|
||||
|
||||
@ -523,7 +523,7 @@ minimumBy cmp = mkFoldl1 min'
|
||||
-- @since 0.7.0
|
||||
{-# INLINE minimum #-}
|
||||
minimum :: (Monad m, Ord a) => Fold m a (Maybe a)
|
||||
minimum = mkFoldl1 min
|
||||
minimum = foldl1 min
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- To Summary (Statistical)
|
||||
@ -535,7 +535,7 @@ minimum = mkFoldl1 min
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE mean #-}
|
||||
mean :: (Monad m, Fractional a) => Fold m a a
|
||||
mean = fmap done $ mkFoldl step begin
|
||||
mean = fmap done $ foldl' step begin
|
||||
|
||||
where
|
||||
|
||||
@ -553,7 +553,7 @@ mean = fmap done $ mkFoldl step begin
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE variance #-}
|
||||
variance :: (Monad m, Fractional a) => Fold m a a
|
||||
variance = fmap done $ mkFoldl step begin
|
||||
variance = fmap done $ foldl' step begin
|
||||
|
||||
where
|
||||
|
||||
@ -592,7 +592,7 @@ stdDev = sqrt <$> variance
|
||||
-- /Pre-release/
|
||||
{-# INLINABLE rollingHashWithSalt #-}
|
||||
rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
|
||||
rollingHashWithSalt = mkFoldl step
|
||||
rollingHashWithSalt = foldl' step
|
||||
|
||||
where
|
||||
|
||||
@ -634,13 +634,13 @@ rollingHashFirstN n = take n rollingHash
|
||||
-- Sum {getSum = 65}
|
||||
--
|
||||
-- @
|
||||
-- sconcat = Fold.mkFoldl (<>)
|
||||
-- sconcat = Fold.foldl' (<>)
|
||||
-- @
|
||||
--
|
||||
-- @since 0.8.0
|
||||
{-# INLINE sconcat #-}
|
||||
sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
|
||||
sconcat = mkFoldl (<>)
|
||||
sconcat = foldl' (<>)
|
||||
|
||||
-- | Fold an input stream consisting of monoidal elements using 'mappend'
|
||||
-- and 'mempty'.
|
||||
@ -690,7 +690,7 @@ foldMap f = lmap f mconcat
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE foldMapM #-}
|
||||
foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
|
||||
foldMapM act = mkFoldlM step (pure mempty)
|
||||
foldMapM act = foldlM' step (pure mempty)
|
||||
|
||||
where
|
||||
|
||||
@ -708,7 +708,7 @@ foldMapM act = mkFoldlM step (pure mempty)
|
||||
|
||||
-- | Buffers the input stream to a list in the reverse order of the input.
|
||||
--
|
||||
-- > toListRev = Fold.mkFoldl (flip (:)) []
|
||||
-- > toListRev = Fold.foldl' (flip (:)) []
|
||||
--
|
||||
-- /Warning!/ working on large lists accumulated as buffers in memory could be
|
||||
-- very inefficient, consider using "Streamly.Array" instead.
|
||||
@ -718,7 +718,7 @@ foldMapM act = mkFoldlM step (pure mempty)
|
||||
-- xn : ... : x2 : x1 : []
|
||||
{-# INLINABLE toListRev #-}
|
||||
toListRev :: Monad m => Fold m a [a]
|
||||
toListRev = mkFoldl (flip (:)) []
|
||||
toListRev = foldl' (flip (:)) []
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Partial Folds
|
||||
@ -1101,7 +1101,7 @@ tee = teeWith (,)
|
||||
-- @since 0.7.0
|
||||
{-# INLINE distribute #-}
|
||||
distribute :: Monad m => [Fold m a b] -> Fold m a [b]
|
||||
distribute = foldr (teeWith (:)) (yield [])
|
||||
distribute = Prelude.foldr (teeWith (:)) (yield [])
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Partitioning
|
||||
@ -1495,7 +1495,7 @@ demuxDefault = demuxDefaultWith id
|
||||
{-# INLINE classifyWith #-}
|
||||
classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
|
||||
classifyWith f (Fold step1 initial1 extract1) =
|
||||
rmapM extract $ mkFoldlM step initial
|
||||
rmapM extract $ foldlM' step initial
|
||||
|
||||
where
|
||||
|
||||
@ -1764,12 +1764,12 @@ chunksBetween _low _high _f1 _f2 = undefined
|
||||
-- /Warning!/ working on large streams accumulated as buffers in memory could
|
||||
-- be very inefficient, consider using "Streamly.Data.Array" instead.
|
||||
--
|
||||
-- > toStream = mkFoldr K.cons K.nil
|
||||
-- > toStream = foldr K.cons K.nil
|
||||
--
|
||||
-- /Pre-release/
|
||||
{-# INLINE toStream #-}
|
||||
toStream :: Monad m => Fold m a (SerialT Identity a)
|
||||
toStream = mkFoldr K.cons K.nil
|
||||
toStream = foldr K.cons K.nil
|
||||
|
||||
-- This is more efficient than 'toStream'. toStream is exactly the same as
|
||||
-- reversing the stream after toStreamRev.
|
||||
@ -1777,7 +1777,7 @@ toStream = mkFoldr K.cons K.nil
|
||||
-- | Buffers the input stream to a pure stream in the reverse order of the
|
||||
-- input.
|
||||
--
|
||||
-- > toStreamRev = mkFoldl (flip K.cons) K.nil
|
||||
-- > toStreamRev = foldl' (flip K.cons) K.nil
|
||||
--
|
||||
-- /Warning!/ working on large streams accumulated as buffers in memory could
|
||||
-- be very inefficient, consider using "Streamly.Data.Array" instead.
|
||||
@ -1787,4 +1787,4 @@ toStream = mkFoldr K.cons K.nil
|
||||
-- xn : ... : x2 : x1 : []
|
||||
{-# INLINABLE toStreamRev #-}
|
||||
toStreamRev :: Monad m => Fold m a (SerialT Identity a)
|
||||
toStreamRev = mkFoldl (flip K.cons) K.nil
|
||||
toStreamRev = foldl' (flip K.cons) K.nil
|
||||
|
@ -196,11 +196,11 @@ module Streamly.Internal.Data.Fold.Type
|
||||
, Fold (..)
|
||||
|
||||
-- * Constructors
|
||||
, mkFoldl
|
||||
, mkFoldlM
|
||||
, mkFoldl1
|
||||
, mkFoldr
|
||||
, mkFoldrM
|
||||
, foldl'
|
||||
, foldlM'
|
||||
, foldl1
|
||||
, foldr
|
||||
, foldrM
|
||||
, mkFold
|
||||
, mkFold_
|
||||
, mkFoldM
|
||||
@ -280,7 +280,7 @@ import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
|
||||
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
|
||||
import Streamly.Internal.Data.SVar (MonadAsync)
|
||||
|
||||
import Prelude hiding (concatMap, filter, map, take)
|
||||
import Prelude hiding (concatMap, filter, foldl1, foldr, map, take)
|
||||
|
||||
-- $setup
|
||||
-- >>> :m
|
||||
@ -397,22 +397,22 @@ rmapM f (Fold step initial extract) = Fold step1 initial1 (extract >=> f)
|
||||
-- the accumulator.
|
||||
--
|
||||
-- If your 'Fold' returns only 'Partial' (i.e. never returns a 'Done') then you
|
||||
-- can use @mkFoldl*@ constructors.
|
||||
-- can use @foldl'*@ constructors.
|
||||
--
|
||||
-- A fold with an extract function can be expressed using fmap:
|
||||
--
|
||||
-- @
|
||||
-- mkfoldlx :: Monad m => (s -> a -> s) -> s -> (s -> b) -> Fold m a b
|
||||
-- mkfoldlx step initial extract = fmap extract (mkFoldl step initial)
|
||||
-- mkfoldlx step initial extract = fmap extract (foldl' step initial)
|
||||
-- @
|
||||
--
|
||||
-- See also: "Streamly.Prelude.foldl'"
|
||||
--
|
||||
-- @since 0.8.0
|
||||
--
|
||||
{-# INLINE mkFoldl #-}
|
||||
mkFoldl :: Monad m => (b -> a -> b) -> b -> Fold m a b
|
||||
mkFoldl step initial =
|
||||
{-# INLINE foldl' #-}
|
||||
foldl' :: Monad m => (b -> a -> b) -> b -> Fold m a b
|
||||
foldl' step initial =
|
||||
Fold
|
||||
(\s a -> return $ Partial $ step s a)
|
||||
(return (Partial initial))
|
||||
@ -425,16 +425,16 @@ mkFoldl step initial =
|
||||
--
|
||||
-- @
|
||||
-- mkFoldlxM :: Functor m => (s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
|
||||
-- mkFoldlxM step initial extract = rmapM extract (mkFoldlM step initial)
|
||||
-- mkFoldlxM step initial extract = rmapM extract (foldlM' step initial)
|
||||
-- @
|
||||
--
|
||||
-- See also: "Streamly.Prelude.foldlM'"
|
||||
--
|
||||
-- @since 0.8.0
|
||||
--
|
||||
{-# INLINE mkFoldlM #-}
|
||||
mkFoldlM :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
|
||||
mkFoldlM step initial =
|
||||
{-# INLINE foldlM' #-}
|
||||
foldlM' :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
|
||||
foldlM' step initial =
|
||||
Fold (\s a -> Partial <$> step s a) (Partial <$> initial) return
|
||||
|
||||
-- | Make a strict left fold, for non-empty streams, using first element as the
|
||||
@ -443,9 +443,9 @@ mkFoldlM step initial =
|
||||
-- See also: "Streamly.Prelude.foldl1'"
|
||||
--
|
||||
-- /Pre-release/
|
||||
{-# INLINE mkFoldl1 #-}
|
||||
mkFoldl1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
|
||||
mkFoldl1 step = fmap toMaybe $ mkFoldl step1 Nothing'
|
||||
{-# INLINE foldl1 #-}
|
||||
foldl1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
|
||||
foldl1 step = fmap toMaybe $ foldl' step1 Nothing'
|
||||
|
||||
where
|
||||
|
||||
@ -463,31 +463,31 @@ mkFoldl1 step = fmap toMaybe $ mkFoldl step1 Nothing'
|
||||
--
|
||||
-- For example,
|
||||
--
|
||||
-- > toList = mkFoldr (:) []
|
||||
-- > toList = foldr (:) []
|
||||
--
|
||||
-- See also: "Streamly.Prelude.foldr"
|
||||
--
|
||||
-- @since 0.8.0
|
||||
{-# INLINE mkFoldr #-}
|
||||
mkFoldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
|
||||
mkFoldr g z = fmap ($ z) $ mkFoldl (\f x -> f . g x) id
|
||||
{-# INLINE foldr #-}
|
||||
foldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
|
||||
foldr g z = fmap ($ z) $ foldl' (\f x -> f . g x) id
|
||||
|
||||
-- XXX we have not seen any use of this yet, not releasing until we have a use
|
||||
-- case.
|
||||
--
|
||||
-- | Like 'mkFoldr' but with a monadic step function.
|
||||
-- | Like 'foldr' but with a monadic step function.
|
||||
--
|
||||
-- For example,
|
||||
--
|
||||
-- > toList = mkFoldrM (\a xs -> return $ a : xs) (return [])
|
||||
-- > toList = foldrM (\a xs -> return $ a : xs) (return [])
|
||||
--
|
||||
-- See also: "Streamly.Prelude.foldrM"
|
||||
--
|
||||
-- /Pre-release/
|
||||
{-# INLINE mkFoldrM #-}
|
||||
mkFoldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
|
||||
mkFoldrM g z =
|
||||
rmapM (z >>=) $ mkFoldlM (\f x -> return $ g x >=> f) (return return)
|
||||
{-# INLINE foldrM #-}
|
||||
foldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
|
||||
foldrM g z =
|
||||
rmapM (z >>=) $ foldlM' (\f x -> return $ g x >=> f) (return return)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- General fold constructors
|
||||
@ -498,7 +498,7 @@ mkFoldrM g z =
|
||||
--
|
||||
-- mkFold :: Monad m => (s -> a -> Step s b) -> Step s b -> Fold m a b
|
||||
--
|
||||
-- Then similar to mkFoldl and mkFoldr we can just fmap extract on it to extend
|
||||
-- Then similar to foldl' and foldr we can just fmap extract on it to extend
|
||||
-- it to the version where an 'extract' function is required. Or do we even
|
||||
-- need that?
|
||||
--
|
||||
@ -586,7 +586,7 @@ simplify (Fold2 step inject extract) c =
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE drain #-}
|
||||
drain :: Monad m => Fold m a ()
|
||||
drain = mkFoldl (\_ _ -> ()) ()
|
||||
drain = foldl' (\_ _ -> ()) ()
|
||||
|
||||
-- | Folds the input stream to a list.
|
||||
--
|
||||
@ -594,12 +594,12 @@ drain = mkFoldl (\_ _ -> ()) ()
|
||||
-- very inefficient, consider using "Streamly.Data.Array.Foreign"
|
||||
-- instead.
|
||||
--
|
||||
-- > toList = mkFoldr (:) []
|
||||
-- > toList = foldr (:) []
|
||||
--
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE toList #-}
|
||||
toList :: Monad m => Fold m a [a]
|
||||
toList = mkFoldr (:) []
|
||||
toList = foldr (:) []
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Instances
|
||||
|
@ -1165,7 +1165,7 @@ transformCombineOpsCommon constr desc eq t = do
|
||||
monadicIO $ do
|
||||
cref <- run $ newIORef 0
|
||||
let fldstp _ e = modifyIORef' cref (e +)
|
||||
sumfoldinref = FL.mkFoldlM fldstp (return ())
|
||||
sumfoldinref = FL.foldlM' fldstp (return ())
|
||||
op = S.tap sumfoldinref . S.mapM (\x -> return (x+1))
|
||||
listOp = fmap (+1)
|
||||
stream <- run ((S.toList . t) $ op (constr a <> constr b))
|
||||
|
Loading…
Reference in New Issue
Block a user