on master help sign in
Rename the smart constructors of terminating folds
commitd4c20981e633d0be905c6be6d47d6cae02993615 authorAdithya Kumar <adithya@composewell.com> dateSun 5 Jun 2022, 04:00 (4 years ago) parent3773fca
3 files changed+22−52
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+3−3 core/src/Streamly/Internal/Data/Fold.hs+11−13 core/src/Streamly/Internal/Data/Fold/Type.hs+8−36
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+3−3
@@ -575,13 +575,13 @@ putIndex i x arr =
575575{-# INLINE putIndices #-}
576576putIndices :: forall m a. (MonadIO m, Storable a)
577577 => Array a -> Fold m (Int, a) ()
578−putIndices Array{..} = FL.mkFoldM step initial extract
578+putIndices Array{..} = FL.rmapM extract $ FL.foldlM' step initial
579579
580580 where
581581
582− initial = return $ FL.Partial ()
582+ initial = return ()
583583
584− step () (i, x) = FL.Partial <$> liftIO (putIndexPtr arrStart aEnd i x)
584+ step () (i, x) = liftIO (putIndexPtr arrStart aEnd i x)
585585
586586 extract () = liftIO $ touch arrContents
587587
core/src/Streamly/Internal/Data/Fold.hs+11−13
@@ -31,12 +31,10 @@ module Streamly.Internal.Data.Fold
3131 , foldl'
3232 , foldlM'
3333 , foldl1'
34+ , foldt'
35+ , foldtM'
3436 , foldr
3537 , foldrM
36− , mkFold
37− , mkFold_
38− , mkFoldM
39− , mkFoldM_
4038
4139 -- * Folds
4240 -- ** Identity
@@ -700,7 +698,7 @@ sum = foldl' (+) 0
700698-- /Since 0.8.0 (Added 'Eq' constraint)/
701699{-# INLINE product #-}
702700product :: (Monad m, Num a, Eq a) => Fold m a a
703−product = mkFold_ step (Partial 1)
701+product = foldt' step (Partial 1) id
704702
705703 where
706704
@@ -1005,7 +1003,7 @@ drainN n = take n drain
10051003-- /Pre-release/
10061004{-# INLINE genericIndex #-}
10071005genericIndex :: (Integral i, Monad m) => i -> Fold m a (Maybe a)
1008−genericIndex i = mkFold step (Partial 0) (const Nothing)
1006+genericIndex i = foldt' step (Partial 0) (const Nothing)
10091007
10101008 where
10111009
@@ -1029,7 +1027,7 @@ index = genericIndex
10291027--
10301028{-# INLINE maybe #-}
10311029maybe :: Monad m => (a -> Maybe b) -> Fold m a (Maybe b)
1032−maybe f = mkFold_ (const (Done . f)) (Partial Nothing)
1030+maybe f = foldt' (const (Done . f)) (Partial Nothing) id
10331031
10341032-- Naming notes:
10351033--
@@ -1079,7 +1077,7 @@ head = one
10791077-- @since 0.7.0
10801078{-# INLINE find #-}
10811079find :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
1082−find predicate = mkFold step (Partial ()) (const Nothing)
1080+find predicate = foldt' step (Partial ()) (const Nothing)
10831081
10841082 where
10851083
@@ -1096,7 +1094,7 @@ find predicate = mkFold step (Partial ()) (const Nothing)
10961094-- @since 0.7.0
10971095{-# INLINE lookup #-}
10981096lookup :: (Eq a, Monad m) => a -> Fold m (a,b) (Maybe b)
1099−lookup a0 = mkFold step (Partial ()) (const Nothing)
1097+lookup a0 = foldt' step (Partial ()) (const Nothing)
11001098
11011099 where
11021100
@@ -1110,7 +1108,7 @@ lookup a0 = mkFold step (Partial ()) (const Nothing)
11101108-- @since 0.7.0
11111109{-# INLINE findIndex #-}
11121110findIndex :: Monad m => (a -> Bool) -> Fold m a (Maybe Int)
1113−findIndex predicate = mkFold step (Partial 0) (const Nothing)
1111+findIndex predicate = foldt' step (Partial 0) (const Nothing)
11141112
11151113 where
11161114
@@ -1156,7 +1154,7 @@ elemIndex a = findIndex (a ==)
11561154-- @since 0.7.0
11571155{-# INLINE null #-}
11581156null :: Monad m => Fold m a Bool
1159−null = mkFold (\() _ -> Done False) (Partial ()) (const True)
1157+null = foldt' (\() _ -> Done False) (Partial ()) (const True)
11601158
11611159-- | Returns 'True' if any of the elements of a stream satisfies a predicate.
11621160--
@@ -1168,7 +1166,7 @@ null = mkFold (\() _ -> Done False) (Partial ()) (const True)
11681166-- @since 0.7.0
11691167{-# INLINE any #-}
11701168any :: Monad m => (a -> Bool) -> Fold m a Bool
1171−any predicate = mkFold_ step initial
1169+any predicate = foldt' step initial id
11721170
11731171 where
11741172
@@ -1198,7 +1196,7 @@ elem a = any (a ==)
11981196-- @since 0.7.0
11991197{-# INLINE all #-}
12001198all :: Monad m => (a -> Bool) -> Fold m a Bool
1201−all predicate = mkFold_ step initial
1199+all predicate = foldt' step initial id
12021200
12031201 where
12041202
core/src/Streamly/Internal/Data/Fold/Type.hs+8−36
@@ -327,12 +327,10 @@ module Streamly.Internal.Data.Fold.Type
327327 , foldl'
328328 , foldlM'
329329 , foldl1'
330+ , foldt'
331+ , foldtM'
330332 , foldr
331333 , foldrM
332− , mkFold
333− , mkFold_
334− , mkFoldM
335− , mkFoldM_
336334
337335 -- * Folds
338336 , fromPure
@@ -597,24 +595,11 @@ foldrM g z =
597595--
598596-- /Pre-release/
599597--
600−{-# INLINE mkFold #-}
601−mkFold :: Monad m => (s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
602−mkFold step initial extract =
598+{-# INLINE foldt' #-}
599+foldt' :: Monad m => (s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
600+foldt' step initial extract =
603601 Fold (\s a -> return $ step s a) (return initial) (return . extract)
604602
605−-- | Similar to 'mkFold' but the final state extracted is identical to the
606−-- intermediate state.
607−--
608−-- @
609−-- mkFold_ step initial = mkFold step initial id
610−-- @
611−--
612−-- /Pre-release/
613−--
614−{-# INLINE mkFold_ #-}
615−mkFold_ :: Monad m => (b -> a -> Step b b) -> Step b b -> Fold m a b
616−mkFold_ step initial = mkFold step initial id
617−
618603-- | Make a terminating fold with an effectful step function and initial state,
619604-- and a state extraction function.
620605--
@@ -624,22 +609,9 @@ mkFold_ step initial = mkFold step initial id
624609--
625610-- /Pre-release/
626611--
627−{-# INLINE mkFoldM #-}
628−mkFoldM :: (s -> a -> m (Step s b)) -> m (Step s b) -> (s -> m b) -> Fold m a b
629−mkFoldM = Fold
630−
631−-- | Similar to 'mkFoldM' but the final state extracted is identical to the
632−-- intermediate state.
633−--
634−-- @
635−-- mkFoldM_ step initial = mkFoldM step initial return
636−-- @
637−--
638−-- /Pre-release/
639−--
640−{-# INLINE mkFoldM_ #-}
641−mkFoldM_ :: Monad m => (b -> a -> m (Step b b)) -> m (Step b b) -> Fold m a b
642−mkFoldM_ step initial = mkFoldM step initial return
612+{-# INLINE foldtM' #-}
613+foldtM' :: (s -> a -> m (Step s b)) -> m (Step s b) -> (s -> m b) -> Fold m a b
614+foldtM' = Fold
643615
644616------------------------------------------------------------------------------
645617-- Refold