mirror of
https://github.com/ilyakooo0/streamly.git
synced 2024-09-11 08:25:40 +03:00
Remove mkAccum* fold constructors
This commit is contained in:
parent
e9a0e7c118
commit
bb72e18365
@ -885,7 +885,8 @@ writeChunks = undefined -- Fold.many Fold.toStream (Array.writeN n)
|
||||
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.mkAccumM step initial extract
|
||||
toArrayMinChunk alignSize elemCount =
|
||||
FL.rmapM extract $ FL.mkFoldlM 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.mkAccumM step initial extract
|
||||
write = FL.rmapM extract $ FL.mkFoldlM 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.mkAccumM step initial extract
|
||||
writeNUnsafe n = FL.rmapM extract $ FL.mkFoldlM step initial
|
||||
|
||||
where
|
||||
|
||||
|
@ -18,10 +18,10 @@ module Streamly.Internal.Data.Fold
|
||||
, Fold (..)
|
||||
|
||||
-- * Creating
|
||||
, mkAccum
|
||||
, mkAccum_
|
||||
, mkAccumM
|
||||
, mkAccumM_
|
||||
, mkFoldl
|
||||
, mkFoldlM
|
||||
, mkFoldr
|
||||
, mkFoldrM
|
||||
, mkFold
|
||||
, mkFold_
|
||||
, mkFoldM
|
||||
@ -100,7 +100,6 @@ module Streamly.Internal.Data.Fold
|
||||
, runStep
|
||||
|
||||
-- * Output Transformations
|
||||
, rsequence
|
||||
, sequence
|
||||
, rmapM
|
||||
, mapM
|
||||
@ -218,7 +217,7 @@ module Streamly.Internal.Data.Fold
|
||||
)
|
||||
where
|
||||
|
||||
import Control.Monad (void, join)
|
||||
import Control.Monad (void)
|
||||
import Data.Bifunctor (first)
|
||||
import Data.Functor.Identity (Identity(..))
|
||||
import Data.Int (Int64)
|
||||
@ -281,30 +280,10 @@ generally = hoist (return . runIdentity)
|
||||
-- Transformations on fold inputs
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- | Flatten the monadic output of a fold to pure output.
|
||||
--
|
||||
-- @since 0.8.0
|
||||
{-# INLINE rsequence #-}
|
||||
rsequence :: Monad m => Fold m a (m b) -> Fold m a b
|
||||
rsequence (Fold step initial extract) = Fold step' initial1 extract'
|
||||
|
||||
where
|
||||
|
||||
eval res =
|
||||
case res of
|
||||
Partial x -> return $ Partial x
|
||||
Done b -> Done <$> b
|
||||
|
||||
initial1 = initial >>= eval
|
||||
|
||||
step' s a = step s a >>= eval
|
||||
|
||||
extract' = join . extract
|
||||
|
||||
-- | Flatten the monadic output of a fold to pure output.
|
||||
--
|
||||
-- @since 0.7.0
|
||||
{-# DEPRECATED sequence "Use rsequence instead" #-}
|
||||
{-# DEPRECATED sequence "Use \"rmapM id\" instead" #-}
|
||||
{-# INLINE sequence #-}
|
||||
sequence :: Monad m => Fold m a (m b) -> Fold m a b
|
||||
sequence = rmapM id
|
||||
@ -386,7 +365,7 @@ transform (Pipe pstep1 pstep2 pinitial) (Fold fstep finitial fextract) =
|
||||
-- from the 'Foldable', the result is 'None' for empty containers.
|
||||
{-# INLINABLE _Fold1 #-}
|
||||
_Fold1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
|
||||
_Fold1 step = mkAccum step_ Nothing' toMaybe
|
||||
_Fold1 step = fmap toMaybe $ mkFoldl step_ Nothing'
|
||||
|
||||
where
|
||||
|
||||
@ -440,7 +419,7 @@ last = _Fold1 (\_ x -> x)
|
||||
-- @since 0.7.0
|
||||
{-# INLINE genericLength #-}
|
||||
genericLength :: (Monad m, Num b) => Fold m a b
|
||||
genericLength = mkAccum_ (\n _ -> n + 1) 0
|
||||
genericLength = mkFoldl (\n _ -> n + 1) 0
|
||||
|
||||
-- | Determine the length of the input stream.
|
||||
--
|
||||
@ -460,7 +439,7 @@ length = genericLength
|
||||
-- @since 0.7.0
|
||||
{-# INLINE sum #-}
|
||||
sum :: (Monad m, Num a) => Fold m a a
|
||||
sum = mkAccum_ (+) 0
|
||||
sum = mkFoldl (+) 0
|
||||
|
||||
-- | Determine the product of all elements of a stream of numbers. Returns
|
||||
-- multiplicative identity (@1@) when the stream is empty. The fold terminates
|
||||
@ -552,7 +531,7 @@ minimum = _Fold1 min
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE mean #-}
|
||||
mean :: (Monad m, Fractional a) => Fold m a a
|
||||
mean = mkAccum step begin done
|
||||
mean = fmap done $ mkFoldl step begin
|
||||
|
||||
where
|
||||
|
||||
@ -570,7 +549,7 @@ mean = mkAccum step begin done
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE variance #-}
|
||||
variance :: (Monad m, Fractional a) => Fold m a a
|
||||
variance = mkAccum step begin done
|
||||
variance = fmap done $ mkFoldl step begin
|
||||
|
||||
where
|
||||
|
||||
@ -609,7 +588,7 @@ stdDev = sqrt <$> variance
|
||||
-- /Pre-release/
|
||||
{-# INLINABLE rollingHashWithSalt #-}
|
||||
rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
|
||||
rollingHashWithSalt = mkAccum_ step
|
||||
rollingHashWithSalt = mkFoldl step
|
||||
|
||||
where
|
||||
|
||||
@ -653,7 +632,7 @@ rollingHashFirstN n = take n rollingHash
|
||||
--
|
||||
{-# INLINE sconcat #-}
|
||||
sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
|
||||
sconcat = mkAccum_ (<>)
|
||||
sconcat = mkFoldl (<>)
|
||||
|
||||
-- | Fold an input stream consisting of monoidal elements using 'mappend'
|
||||
-- and 'mempty'.
|
||||
@ -698,7 +677,7 @@ foldMap f = map f mconcat
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE foldMapM #-}
|
||||
foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
|
||||
foldMapM act = mkAccumM_ step (pure mempty)
|
||||
foldMapM act = mkFoldlM step (pure mempty)
|
||||
|
||||
where
|
||||
|
||||
@ -724,7 +703,7 @@ foldMapM act = mkAccumM_ step (pure mempty)
|
||||
-- xn : ... : x2 : x1 : []
|
||||
{-# INLINABLE toListRev #-}
|
||||
toListRev :: Monad m => Fold m a [a]
|
||||
toListRev = mkAccum_ (flip (:)) []
|
||||
toListRev = mkFoldl (flip (:)) []
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Partial Folds
|
||||
@ -1525,7 +1504,8 @@ 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) = mkAccumM step initial extract
|
||||
classifyWith f (Fold step1 initial1 extract1) =
|
||||
rmapM extract $ mkFoldlM step initial
|
||||
|
||||
where
|
||||
|
||||
@ -1812,4 +1792,4 @@ toStream = mkFoldr K.cons K.nil
|
||||
-- xn : ... : x2 : x1 : []
|
||||
{-# INLINABLE toStreamRev #-}
|
||||
toStreamRev :: Monad m => Fold m a (SerialT Identity a)
|
||||
toStreamRev = mkAccum_ (flip K.cons) K.nil
|
||||
toStreamRev = mkFoldl (flip K.cons) K.nil
|
||||
|
@ -176,10 +176,6 @@ module Streamly.Internal.Data.Fold.Types
|
||||
, mkFoldlM
|
||||
, mkFoldr
|
||||
, mkFoldrM
|
||||
, mkAccum
|
||||
, mkAccum_
|
||||
, mkAccumM
|
||||
, mkAccumM_
|
||||
, mkFold
|
||||
, mkFold_
|
||||
, mkFoldM
|
||||
@ -353,6 +349,16 @@ rmapM f (Fold step initial extract) = Fold step1 initial1 (extract >=> f)
|
||||
-- | Make a fold from a left fold style pure step function and initial value of
|
||||
-- the accumulator.
|
||||
--
|
||||
-- If your 'Fold' returns only 'Partial' (i.e. never returns a 'Done') then you
|
||||
-- can use @mkFoldl*@ 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)
|
||||
-- @
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE mkFoldl #-}
|
||||
@ -366,6 +372,13 @@ mkFoldl step initial =
|
||||
-- | Make a fold from a left fold style monadic step function and initial value
|
||||
-- of the accumulator.
|
||||
--
|
||||
-- A fold with an extract function can be expressed using rmapM:
|
||||
--
|
||||
-- @
|
||||
-- mkAccumM :: Functor m => (s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
|
||||
-- mkAccumM step initial extract = rmapM extract (mkFoldlM step initial)
|
||||
-- @
|
||||
--
|
||||
-- /Pre-release/
|
||||
--
|
||||
{-# INLINE mkFoldlM #-}
|
||||
@ -373,62 +386,6 @@ mkFoldlM :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
|
||||
mkFoldlM step initial =
|
||||
Fold (\s a -> Partial <$> step s a) (Partial <$> initial) return
|
||||
|
||||
-- | Make an accumulating (non-terminating) fold using a pure step function, a
|
||||
-- pure initial state and a pure state extraction function.
|
||||
--
|
||||
-- If your 'Fold' returns only 'Partial' (i.e. never returns a 'Done') then you
|
||||
-- can use @mkAccum*@ constructors.
|
||||
--
|
||||
-- > mkAccum step initial extract = fmap extract (mkFoldl step initial)
|
||||
--
|
||||
-- /Internal/
|
||||
--
|
||||
{-# INLINE mkAccum #-}
|
||||
mkAccum :: Monad m => (s -> a -> s) -> s -> (s -> b) -> Fold m a b
|
||||
mkAccum step initial extract =
|
||||
Fold
|
||||
(\s a -> return $ Partial $ step s a)
|
||||
(return (Partial initial))
|
||||
(return . extract)
|
||||
|
||||
-- | Similar to 'mkAccum' but the final state extracted is identical to the
|
||||
-- intermediate state.
|
||||
--
|
||||
-- @
|
||||
-- mkAccum_ step initial = mkAccum step initial id
|
||||
-- @
|
||||
--
|
||||
-- /Internal/
|
||||
--
|
||||
{-# INLINE mkAccum_ #-}
|
||||
mkAccum_ :: Monad m => (b -> a -> b) -> b -> Fold m a b
|
||||
mkAccum_ = mkFoldl
|
||||
|
||||
-- | Make an accumulating (non-terminating) fold with an effectful step
|
||||
-- function, an initial state, and a state extraction function.
|
||||
--
|
||||
-- > mkAccumM step initial extract = rmapM extract (mkFoldlM step initial)
|
||||
--
|
||||
-- /Internal/
|
||||
--
|
||||
{-# INLINE mkAccumM #-}
|
||||
mkAccumM :: Functor m => (s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
|
||||
mkAccumM step initial =
|
||||
Fold (\s a -> Partial <$> step s a) (Partial <$> initial)
|
||||
|
||||
-- | Similar to 'mkAccumM' but the final state extracted is identical to the
|
||||
-- intermediate state.
|
||||
--
|
||||
-- @
|
||||
-- mkAccumM_ step initial = mkAccumM step initial return
|
||||
-- @
|
||||
--
|
||||
-- /Internal/
|
||||
--
|
||||
{-# INLINE mkAccumM_ #-}
|
||||
mkAccumM_ :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
|
||||
mkAccumM_ = mkFoldlM
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Right fold constructors
|
||||
------------------------------------------------------------------------------
|
||||
@ -446,7 +403,7 @@ mkAccumM_ = mkFoldlM
|
||||
-- /Pre-release/
|
||||
{-# INLINE mkFoldr #-}
|
||||
mkFoldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
|
||||
mkFoldr g z = mkAccum (\f x -> f . g x) id ($ z)
|
||||
mkFoldr g z = fmap ($ z) $ mkFoldl (\f x -> f . g x) id
|
||||
|
||||
-- | Like 'mkFoldr' but with a monadic step function.
|
||||
--
|
||||
@ -457,7 +414,8 @@ mkFoldr g z = mkAccum (\f x -> f . g x) id ($ z)
|
||||
-- /Pre-release/
|
||||
{-# INLINE mkFoldrM #-}
|
||||
mkFoldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
|
||||
mkFoldrM g z = mkAccumM (\f x -> return $ g x >=> f) (return return) (z >>=)
|
||||
mkFoldrM g z =
|
||||
rmapM (z >>=) $ mkFoldlM (\f x -> return $ g x >=> f) (return return)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- General fold constructors
|
||||
@ -545,7 +503,7 @@ simplify (Fold2 step inject extract) c =
|
||||
-- @since 0.7.0
|
||||
{-# INLINABLE drain #-}
|
||||
drain :: Monad m => Fold m a ()
|
||||
drain = mkAccum_ (\_ _ -> ()) ()
|
||||
drain = mkFoldl (\_ _ -> ()) ()
|
||||
|
||||
-- | Folds the input stream to a list.
|
||||
--
|
||||
|
@ -1166,7 +1166,7 @@ transformCombineOpsCommon constr desc eq t = do
|
||||
monadicIO $ do
|
||||
cref <- run $ newIORef 0
|
||||
let fldstp _ e = modifyIORef' cref (e +)
|
||||
sumfoldinref = FL.mkAccumM_ fldstp (return ())
|
||||
sumfoldinref = FL.mkFoldlM 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