on master help sign in
Remove mkAccum* fold constructors
commitbb72e18365fcfeb82a6f47dfdf94ccfd46d6fe97 authorHarendra Kumar <harendra@composewell.com> dateWed 10 Mar 2021, 13:55 (6 years ago) parente9a0e7c
5 files changed+44−105
src/Streamly/Internal/Data/Array/Foreign/Mut/Types.hs+2−1 src/Streamly/Internal/Data/Array/Prim/MutTypesInclude.hs+2−2 src/Streamly/Internal/Data/Fold.hs+18−38 src/Streamly/Internal/Data/Fold/Types.hs+21−63 test/lib/Streamly/Test/Prelude/Common.hs+1−1
src/Streamly/Internal/Data/Array/Foreign/Mut/Types.hs+2−1
@@ -885,7 +885,8 @@ writeChunks = undefined -- Fold.many Fold.toStream (Array.writeN n)
885885toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
886886 => Int -> Int -> Fold m a (Array a)
887887-- 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
889890
890891 where
891892
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.mkAccumM step initial extract
140+write = FL.rmapM extract $ FL.mkFoldlM 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.mkAccumM step initial extract
196+writeNUnsafe n = FL.rmapM extract $ FL.mkFoldlM step initial
197197
198198 where
199199
src/Streamly/Internal/Data/Fold.hs+18−38
@@ -18,10 +18,10 @@ module Streamly.Internal.Data.Fold
1818 , Fold (..)
1919
2020 -- * Creating
21− , mkAccum
22− , mkAccum_
23− , mkAccumM
24− , mkAccumM_
21+ , mkFoldl
22+ , mkFoldlM
23+ , mkFoldr
24+ , mkFoldrM
2525 , mkFold
2626 , mkFold_
2727 , mkFoldM
@@ -100,7 +100,6 @@ module Streamly.Internal.Data.Fold
100100 , runStep
101101
102102 -- * Output Transformations
103− , rsequence
104103 , sequence
105104 , rmapM
106105 , mapM
@@ -218,7 +217,7 @@ module Streamly.Internal.Data.Fold
218217 )
219218where
220219
221−import Control.Monad (void, join)
220+import Control.Monad (void)
222221import Data.Bifunctor (first)
223222import Data.Functor.Identity (Identity(..))
224223import Data.Int (Int64)
@@ -281,30 +280,10 @@ generally = hoist (return . runIdentity)
281280-- Transformations on fold inputs
282281------------------------------------------------------------------------------
283282
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−
304283-- | Flatten the monadic output of a fold to pure output.
305284--
306285-- @since 0.7.0
307−{-# DEPRECATED sequence "Use rsequence instead" #-}
286+{-# DEPRECATED sequence "Use \"rmapM id\" instead" #-}
308287{-# INLINE sequence #-}
309288sequence :: Monad m => Fold m a (m b) -> Fold m a b
310289sequence = rmapM id
@@ -386,7 +365,7 @@ transform (Pipe pstep1 pstep2 pinitial) (Fold fstep finitial fextract) =
386365-- from the 'Foldable', the result is 'None' for empty containers.
387366{-# INLINABLE _Fold1 #-}
388367_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'
390369
391370 where
392371
@@ -440,7 +419,7 @@ last = _Fold1 (\_ x -> x)
440419-- @since 0.7.0
441420{-# INLINE genericLength #-}
442421genericLength :: (Monad m, Num b) => Fold m a b
443−genericLength = mkAccum_ (\n _ -> n + 1) 0
422+genericLength = mkFoldl (\n _ -> n + 1) 0
444423
445424-- | Determine the length of the input stream.
446425--
@@ -460,7 +439,7 @@ length = genericLength
460439-- @since 0.7.0
461440{-# INLINE sum #-}
462441sum :: (Monad m, Num a) => Fold m a a
463−sum = mkAccum_ (+) 0
442+sum = mkFoldl (+) 0
464443
465444-- | Determine the product of all elements of a stream of numbers. Returns
466445-- multiplicative identity (@1@) when the stream is empty. The fold terminates
@@ -552,7 +531,7 @@ minimum = _Fold1 min
552531-- @since 0.7.0
553532{-# INLINABLE mean #-}
554533mean :: (Monad m, Fractional a) => Fold m a a
555−mean = mkAccum step begin done
534+mean = fmap done $ mkFoldl step begin
556535
557536 where
558537
@@ -570,7 +549,7 @@ mean = mkAccum step begin done
570549-- @since 0.7.0
571550{-# INLINABLE variance #-}
572551variance :: (Monad m, Fractional a) => Fold m a a
573−variance = mkAccum step begin done
552+variance = fmap done $ mkFoldl step begin
574553
575554 where
576555
@@ -609,7 +588,7 @@ stdDev = sqrt <$> variance
609588-- /Pre-release/
610589{-# INLINABLE rollingHashWithSalt #-}
611590rollingHashWithSalt :: (Monad m, Enum a) => Int64 -> Fold m a Int64
612−rollingHashWithSalt = mkAccum_ step
591+rollingHashWithSalt = mkFoldl step
613592
614593 where
615594
@@ -653,7 +632,7 @@ rollingHashFirstN n = take n rollingHash
653632--
654633{-# INLINE sconcat #-}
655634sconcat :: (Monad m, Semigroup a) => a -> Fold m a a
656−sconcat = mkAccum_ (<>)
635+sconcat = mkFoldl (<>)
657636
658637-- | Fold an input stream consisting of monoidal elements using 'mappend'
659638-- and 'mempty'.
@@ -698,7 +677,7 @@ foldMap f = map f mconcat
698677-- @since 0.7.0
699678{-# INLINABLE foldMapM #-}
700679foldMapM :: (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)
702681
703682 where
704683
@@ -724,7 +703,7 @@ foldMapM act = mkAccumM_ step (pure mempty)
724703-- xn : ... : x2 : x1 : []
725704{-# INLINABLE toListRev #-}
726705toListRev :: Monad m => Fold m a [a]
727−toListRev = mkAccum_ (flip (:)) []
706+toListRev = mkFoldl (flip (:)) []
728707
729708------------------------------------------------------------------------------
730709-- Partial Folds
@@ -1525,7 +1504,8 @@ demuxDefault = demuxDefaultWith id
15251504--
15261505{-# INLINE classifyWith #-}
15271506classifyWith :: (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
15291509
15301510 where
15311511
@@ -1812,4 +1792,4 @@ toStream = mkFoldr K.cons K.nil
18121792-- xn : ... : x2 : x1 : []
18131793{-# INLINABLE toStreamRev #-}
18141794toStreamRev :: 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
176176 , mkFoldlM
177177 , mkFoldr
178178 , mkFoldrM
179− , mkAccum
180− , mkAccum_
181− , mkAccumM
182− , mkAccumM_
183179 , mkFold
184180 , mkFold_
185181 , mkFoldM
@@ -353,6 +349,16 @@ rmapM f (Fold step initial extract) = Fold step1 initial1 (extract >=> f)
353349-- | Make a fold from a left fold style pure step function and initial value of
354350-- the accumulator.
355351--
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+--
356362-- /Pre-release/
357363--
358364{-# INLINE mkFoldl #-}
@@ -366,6 +372,13 @@ mkFoldl step initial =
366372-- | Make a fold from a left fold style monadic step function and initial value
367373-- of the accumulator.
368374--
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+--
369382-- /Pre-release/
370383--
371384{-# INLINE mkFoldlM #-}
@@ -373,62 +386,6 @@ mkFoldlM :: Monad m => (b -> a -> m b) -> m b -> Fold m a b
373386mkFoldlM step initial =
374387 Fold (\s a -> Partial <$> step s a) (Partial <$> initial) return
375388
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−
432389------------------------------------------------------------------------------
433390-- Right fold constructors
434391------------------------------------------------------------------------------
@@ -446,7 +403,7 @@ mkAccumM_ = mkFoldlM
446403-- /Pre-release/
447404{-# INLINE mkFoldr #-}
448405mkFoldr :: 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
450407
451408-- | Like 'mkFoldr' but with a monadic step function.
452409--
@@ -457,7 +414,8 @@ mkFoldr g z = mkAccum (\f x -> f . g x) id ($ z)
457414-- /Pre-release/
458415{-# INLINE mkFoldrM #-}
459416mkFoldrM :: Monad m => (a -> b -> m b) -> m b -> Fold m a b
460−mkFoldrM g z = mkAccumM (\f x -> return $ g x >=> f) (return return) (z >>=)
417+mkFoldrM g z =
418+ rmapM (z >>=) $ mkFoldlM (\f x -> return $ g x >=> f) (return return)
461419
462420------------------------------------------------------------------------------
463421-- General fold constructors
@@ -545,7 +503,7 @@ simplify (Fold2 step inject extract) c =
545503-- @since 0.7.0
546504{-# INLINABLE drain #-}
547505drain :: Monad m => Fold m a ()
548−drain = mkAccum_ (\_ _ -> ()) ()
506+drain = mkFoldl (\_ _ -> ()) ()
549507
550508-- | Folds the input stream to a list.
551509--
test/lib/Streamly/Test/Prelude/Common.hs+1−1
@@ -1166,7 +1166,7 @@ transformCombineOpsCommon constr desc eq t = do
11661166 monadicIO $ do
11671167 cref <- run $ newIORef 0
11681168 let fldstp _ e = modifyIORef' cref (e +)
1169− sumfoldinref = FL.mkAccumM_ fldstp (return ())
1169+ sumfoldinref = FL.mkFoldlM fldstp (return ())
11701170 op = S.tap sumfoldinref . S.mapM (\x -> return (x+1))
11711171 listOp = fmap (+1)
11721172 stream <- run ((S.toList . t) $ op (constr a <> constr b))