on master help sign in
Remove mkAccum* fold constructors
bb72e18Harendra Kumar
src/Streamly/Internal/Data/Array/Foreign/Mut/Types.hs+2−1
@@ -885,7 +885,8 @@ writeChunks = undefined -- Fold.many Fold.toStream (Array.writeN n)
885toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
885toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
886 => Int -> Int -> Fold m a (Array a)
886 => Int -> Int -> Fold m a (Array a)
887-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
887-- toArrayMinChunk n = FL.rmapM spliceArrays $ toArraysOf n
888−toArrayMinChunk alignSize elemCount = FL.mkAccumM step initial extract
888+toArrayMinChunk alignSize elemCount =
889+ FL.rmapM extract $ FL.mkFoldlM step initial
889
890
890 where
891 where
891
892
src/Streamly/Internal/Data/Array/Prim/MutTypesInclude.hs+2−2
@@ -137,7 +137,7 @@ shrinkArray (Array arr#) (I# n#) =
137-- /Pre-release/
137-- /Pre-release/
138{-# INLINE_NORMAL write #-}
138{-# INLINE_NORMAL write #-}
139write :: (MonadIO m, Prim a) => Fold m a (Array a)
139write :: (MonadIO m, Prim a) => Fold m a (Array a)
140−write = FL.mkAccumM step initial extract
140+write = FL.rmapM extract $ FL.mkFoldlM step initial
141
141
142 where
142 where
143
143
@@ -193,7 +193,7 @@ data ArrayUnsafe a = ArrayUnsafe
193-- /Pre-release/
193-- /Pre-release/
194{-# INLINE_NORMAL writeNUnsafe #-}
194{-# INLINE_NORMAL writeNUnsafe #-}
195writeNUnsafe :: (MonadIO m, Prim a) => Int -> Fold m a (Array a)
195writeNUnsafe :: (MonadIO m, Prim a) => Int -> Fold m a (Array a)
196−writeNUnsafe n = FL.mkAccumM step initial extract
196+writeNUnsafe n = FL.rmapM extract $ FL.mkFoldlM step initial
197
197
198 where
198 where
199
199
src/Streamly/Internal/Data/Fold.hs+18−38
@@ -18,10 +18,10 @@ module Streamly.Internal.Data.Fold
18 , Fold (..)
18 , Fold (..)
19
19
20 -- * Creating
20 -- * Creating
21− , mkAccum
21+ , mkFoldl
22− , mkAccum_
22+ , mkFoldlM
23− , mkAccumM
23+ , mkFoldr
24− , mkAccumM_
24+ , mkFoldrM
25 , mkFold
25 , mkFold
26 , mkFold_
26 , mkFold_
27 , mkFoldM
27 , mkFoldM
@@ -100,7 +100,6 @@ module Streamly.Internal.Data.Fold
100 , runStep
100 , runStep
101
101
102 -- * Output Transformations
102 -- * Output Transformations
103− , rsequence
104 , sequence
103 , sequence
105 , rmapM
104 , rmapM
106 , mapM
105 , mapM
@@ -218,7 +217,7 @@ module Streamly.Internal.Data.Fold
218 )
217 )
219where
218where
220
219
221−import Control.Monad (void, join)
220+import Control.Monad (void)
222import Data.Bifunctor (first)
221import Data.Bifunctor (first)
223import Data.Functor.Identity (Identity(..))
222import Data.Functor.Identity (Identity(..))
224import Data.Int (Int64)
223import Data.Int (Int64)
@@ -281,30 +280,10 @@ generally = hoist (return . runIdentity)
281-- Transformations on fold inputs
280-- Transformations on fold inputs
282------------------------------------------------------------------------------
281------------------------------------------------------------------------------
283
282
284−-- | Flatten the monadic output of a fold to pure output.
285−--
286−-- @since 0.8.0
287−{-# INLINE rsequence #-}
288−rsequence :: Monad m => Fold m a (m b) -> Fold m a b
289−rsequence (Fold step initial extract) = Fold step' initial1 extract'
290−
291− where
292−
293− eval res =
294− case res of
295− Partial x -> return $ Partial x
296− Done b -> Done <$> b
297−
298− initial1 = initial >>= eval
299−
300− step' s a = step s a >>= eval
301−
302− extract' = join . extract
303−
304-- | Flatten the monadic output of a fold to pure output.
283-- | Flatten the monadic output of a fold to pure output.
305--
284--
306-- @since 0.7.0
285-- @since 0.7.0
307−{-# DEPRECATED sequence "Use rsequence instead" #-}
286+{-# DEPRECATED sequence "Use \"rmapM id\" instead" #-}
308{-# INLINE sequence #-}
287{-# INLINE sequence #-}
309sequence :: Monad m => Fold m a (m b) -> Fold m a b
288sequence :: Monad m => Fold m a (m b) -> Fold m a b
310sequence = rmapM id
289sequence = rmapM id
@@ -386,7 +365,7 @@ transform (Pipe pstep1 pstep2 pinitial) (Fold fstep finitial fextract) =
386-- from the 'Foldable', the result is 'None' for empty containers.
365-- from the 'Foldable', the result is 'None' for empty containers.
387{-# INLINABLE _Fold1 #-}
366{-# INLINABLE _Fold1 #-}
388_Fold1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
367_Fold1 :: Monad m => (a -> a -> a) -> Fold m a (Maybe a)
389−_Fold1 step = mkAccum step_ Nothing' toMaybe
368+_Fold1 step = fmap toMaybe $ mkFoldl step_ Nothing'
390
369
391 where
370 where
392
371
@@ -440,7 +419,7 @@ last = _Fold1 (\_ x -> x)
440-- @since 0.7.0
419-- @since 0.7.0
441{-# INLINE genericLength #-}
420{-# INLINE genericLength #-}
442genericLength :: (Monad m, Num b) => Fold m a b
421genericLength :: (Monad m, Num b) => Fold m a b
443−genericLength = mkAccum_ (\n _ -> n + 1) 0
422+genericLength = mkFoldl (\n _ -> n + 1) 0
444
423
445-- | Determine the length of the input stream.
424-- | Determine the length of the input stream.
446--
425--
@@ -460,7 +439,7 @@ length = genericLength
460-- @since 0.7.0
439-- @since 0.7.0
461{-# INLINE sum #-}
440{-# INLINE sum #-}
462sum :: (Monad m, Num a) => Fold m a a
441sum :: (Monad m, Num a) => Fold m a a
463−sum = mkAccum_ (+) 0
442+sum = mkFoldl (+) 0
464
443
465-- | Determine the product of all elements of a stream of numbers. Returns
444-- | Determine the product of all elements of a stream of numbers. Returns
466-- multiplicative identity (@1@) when the stream is empty. The fold terminates
445-- multiplicative identity (@1@) when the stream is empty. The fold terminates
@@ -552,7 +531,7 @@ minimum = _Fold1 min
552-- @since 0.7.0
531-- @since 0.7.0
553{-# INLINABLE mean #-}
532{-# INLINABLE mean #-}
554mean :: (Monad m, Fractional a) => Fold m a a
533mean :: (Monad m, Fractional a) => Fold m a a
555−mean = mkAccum step begin done
534+mean = fmap done $ mkFoldl step begin
556
535
557 where
536 where
558
537
@@ -570,7 +549,7 @@ mean = mkAccum step begin done
570-- @since 0.7.0
549-- @since 0.7.0
571{-# INLINABLE variance #-}
550{-# INLINABLE variance #-}
572variance :: (Monad m, Fractional a) => Fold m a a
551variance :: (Monad m, Fractional a) => Fold m a a
573−variance = mkAccum step begin done
552+variance = fmap done $ mkFoldl step begin
574
553
575 where
554 where
576
555
@@ -609,7 +588,7 @@ stdDev = sqrt <$> variance
609-- /Pre-release/
588-- /Pre-release/
610{-# INLINABLE rollingHashWithSalt #-}
589{-# INLINABLE rollingHashWithSalt #-}
611rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
590rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
612−rollingHashWithSalt = mkAccum_ step
591+rollingHashWithSalt = mkFoldl step
613
592
614 where
593 where
615
594
@@ -653,7 +632,7 @@ rollingHashFirstN n = take n rollingHash
653--
632--
654{-# INLINE sconcat #-}
633{-# INLINE sconcat #-}
655sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
634sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
656−sconcat = mkAccum_ (<>)
635+sconcat = mkFoldl (<>)
657
636
658-- | Fold an input stream consisting of monoidal elements using 'mappend'
637-- | Fold an input stream consisting of monoidal elements using 'mappend'
659-- and 'mempty'.
638-- and 'mempty'.
@@ -698,7 +677,7 @@ foldMap f = map f mconcat
698-- @since 0.7.0
677-- @since 0.7.0
699{-# INLINABLE foldMapM #-}
678{-# INLINABLE foldMapM #-}
700foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
679foldMapM :: (Monad m, Monoid b) => (a -> m b) -> Fold m a b
701−foldMapM act = mkAccumM_ step (pure mempty)
680+foldMapM act = mkFoldlM step (pure mempty)
702
681
703 where
682 where
704
683
@@ -724,7 +703,7 @@ foldMapM act = mkAccumM_ step (pure mempty)
724-- xn : ... : x2 : x1 : []
703-- xn : ... : x2 : x1 : []
725{-# INLINABLE toListRev #-}
704{-# INLINABLE toListRev #-}
726toListRev :: Monad m => Fold m a [a]
705toListRev :: Monad m => Fold m a [a]
727−toListRev = mkAccum_ (flip (:)) []
706+toListRev = mkFoldl (flip (:)) []
728
707
729------------------------------------------------------------------------------
708------------------------------------------------------------------------------
730-- Partial Folds
709-- Partial Folds
@@ -1525,7 +1504,8 @@ demuxDefault = demuxDefaultWith id
1525--
1504--
1526{-# INLINE classifyWith #-}
1505{-# INLINE classifyWith #-}
1527classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1506classifyWith :: (Monad m, Ord k) => (a -> k) -> Fold m a b -> Fold m a (Map k b)
1528−classifyWith f (Fold step1 initial1 extract1) = mkAccumM step initial extract
1507+classifyWith f (Fold step1 initial1 extract1) =
1508+ rmapM extract $ mkFoldlM step initial
1529
1509
1530 where
1510 where
1531
1511
@@ -1812,4 +1792,4 @@ toStream = mkFoldr K.cons K.nil
1812-- xn : ... : x2 : x1 : []
1792-- xn : ... : x2 : x1 : []
1813{-# INLINABLE toStreamRev #-}
1793{-# INLINABLE toStreamRev #-}
1814toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1794toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1815−toStreamRev = mkAccum_ (flip K.cons) K.nil
1795+toStreamRev = mkFoldl (flip K.cons) K.nil
src/Streamly/Internal/Data/Fold/Types.hs+21−63
@@ -176,10 +176,6 @@ module Streamly.Internal.Data.Fold.Types
176 , mkFoldlM
176 , mkFoldlM
177 , mkFoldr
177 , mkFoldr
178 , mkFoldrM
178 , mkFoldrM
179− , mkAccum
180− , mkAccum_
181− , mkAccumM
182− , mkAccumM_
183 , mkFold
179 , mkFold
184 , mkFold_
180 , mkFold_
185 , mkFoldM
181 , mkFoldM
@@ -353,6 +349,16 @@ rmapM f (Fold step initial extract) = Fold step1 initial1 (extract >=> f)
353-- | Make a fold from a left fold style pure step function and initial value of
349-- | Make a fold from a left fold style pure step function and initial value of
354-- the accumulator.
350-- the accumulator.
355--
351--
352+-- If your 'Fold' returns only 'Partial' (i.e. never returns a 'Done') then you
353+-- can use @mkFoldl*@ constructors.
354+--
355+-- A fold with an extract function can be expressed using fmap:
356+--
357+-- @
358+-- mkfoldlx :: Monad m => (s -> a -> s) -> s -> (s -> b) -> Fold m a b
359+-- mkfoldlx step initial extract = fmap extract (mkFoldl step initial)
360+-- @
361+--
356-- /Pre-release/
362-- /Pre-release/
357--
363--
358{-# INLINE mkFoldl #-}
364{-# INLINE mkFoldl #-}
@@ -366,6 +372,13 @@ mkFoldl step initial =
366-- | Make a fold from a left fold style monadic step function and initial value
372-- | Make a fold from a left fold style monadic step function and initial value
367-- of the accumulator.
373-- of the accumulator.
368--
374--
375+-- A fold with an extract function can be expressed using rmapM:
376+--
377+-- @
378+-- mkAccumM :: Functor m => (s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
379+-- mkAccumM step initial extract = rmapM extract (mkFoldlM step initial)
380+-- @
381+--
369-- /Pre-release/
382-- /Pre-release/
370--
383--
371{-# INLINE mkFoldlM #-}
384{-# INLINE mkFoldlM #-}
@@ -373,62 +386,6 @@ mkFoldlM :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
373mkFoldlM step initial =
386mkFoldlM step initial =
374 Fold (\s a -> Partial <$> step s a) (Partial <$> initial) return
387 Fold (\s a -> Partial <$> step s a) (Partial <$> initial) return
375
388
376−-- | Make an accumulating (non-terminating) fold using a pure step function, a
377−-- pure initial state and a pure state extraction function.
378−--
379−-- If your 'Fold' returns only 'Partial' (i.e. never returns a 'Done') then you
380−-- can use @mkAccum*@ constructors.
381−--
382−-- > mkAccum step initial extract = fmap extract (mkFoldl step initial)
383−--
384−-- /Internal/
385−--
386−{-# INLINE mkAccum #-}
387−mkAccum :: Monad m => (s -> a -> s) -> s -> (s -> b) -> Fold m a b
388−mkAccum step initial extract =
389− Fold
390− (\s a -> return $ Partial $ step s a)
391− (return (Partial initial))
392− (return . extract)
393−
394−-- | Similar to 'mkAccum' but the final state extracted is identical to the
395−-- intermediate state.
396−--
397−-- @
398−-- mkAccum_ step initial = mkAccum step initial id
399−-- @
400−--
401−-- /Internal/
402−--
403−{-# INLINE mkAccum_ #-}
404−mkAccum_ :: Monad m => (b -> a -> b) -> b -> Fold m a b
405−mkAccum_ = mkFoldl
406−
407−-- | Make an accumulating (non-terminating) fold with an effectful step
408−-- function, an initial state, and a state extraction function.
409−--
410−-- > mkAccumM step initial extract = rmapM extract (mkFoldlM step initial)
411−--
412−-- /Internal/
413−--
414−{-# INLINE mkAccumM #-}
415−mkAccumM :: Functor m => (s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
416−mkAccumM step initial =
417− Fold (\s a -> Partial <$> step s a) (Partial <$> initial)
418−
419−-- | Similar to 'mkAccumM' but the final state extracted is identical to the
420−-- intermediate state.
421−--
422−-- @
423−-- mkAccumM_ step initial = mkAccumM step initial return
424−-- @
425−--
426−-- /Internal/
427−--
428−{-# INLINE mkAccumM_ #-}
429−mkAccumM_ :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
430−mkAccumM_ = mkFoldlM
431−
432------------------------------------------------------------------------------
389------------------------------------------------------------------------------
433-- Right fold constructors
390-- Right fold constructors
434------------------------------------------------------------------------------
391------------------------------------------------------------------------------
@@ -446,7 +403,7 @@ mkAccumM_ = mkFoldlM
446-- /Pre-release/
403-- /Pre-release/
447{-# INLINE mkFoldr #-}
404{-# INLINE mkFoldr #-}
448mkFoldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
405mkFoldr :: Monad m => (a -> b -> b) -> b -> Fold m a b
449−mkFoldr g z = mkAccum (\f x -> f . g x) id ($ z)
406+mkFoldr g z = fmap ($ z) $ mkFoldl (\f x -> f . g x) id
450
407
451-- | Like 'mkFoldr' but with a monadic step function.
408-- | Like 'mkFoldr' but with a monadic step function.
452--
409--
@@ -457,7 +414,8 @@ mkFoldr g z = mkAccum (\f x -> f . g x) id ($ z)
457-- /Pre-release/
414-- /Pre-release/
458{-# INLINE mkFoldrM #-}
415{-# INLINE mkFoldrM #-}
459mkFoldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
416mkFoldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
417+mkFoldrM g z =
460−mkFoldrM g z = mkAccumM (\f x -> return $ g x >=> f) (return return) (z >>=)
418+ rmapM (z >>=) $ mkFoldlM (\f x -> return $ g x >=> f) (return return)
461
419
462------------------------------------------------------------------------------
420------------------------------------------------------------------------------
463-- General fold constructors
421-- General fold constructors
@@ -545,7 +503,7 @@ simplify (Fold2 step inject extract) c =
545-- @since 0.7.0
503-- @since 0.7.0
546{-# INLINABLE drain #-}
504{-# INLINABLE drain #-}
547drain :: Monad m => Fold m a ()
505drain :: Monad m => Fold m a ()
548−drain = mkAccum_ (\_ _ -> ()) ()
506+drain = mkFoldl (\_ _ -> ()) ()
549
507
550-- | Folds the input stream to a list.
508-- | Folds the input stream to a list.
551--
509--
test/lib/Streamly/Test/Prelude/Common.hs+1−1
@@ -1166,7 +1166,7 @@ transformCombineOpsCommon constr desc eq t = do
1166 monadicIO $ do
1166 monadicIO $ do
1167 cref <- run $ newIORef 0
1167 cref <- run $ newIORef 0
1168 let fldstp _ e = modifyIORef' cref (e +)
1168 let fldstp _ e = modifyIORef' cref (e +)
1169− sumfoldinref = FL.mkAccumM_ fldstp (return ())
1169+ sumfoldinref = FL.mkFoldlM fldstp (return ())
1170 op = S.tap sumfoldinref . S.mapM (\x -> return (x+1))
1170 op = S.tap sumfoldinref . S.mapM (\x -> return (x+1))
1171 listOp = fmap (+1)
1171 listOp = fmap (+1)
1172 stream <- run ((S.toList . t) $ op (constr a <> constr b))
1172 stream <- run ((S.toList . t) $ op (constr a <> constr b))