on master help sign in
Rename the smart constructors of terminating folds
d4c2098Adithya Kumar
core/src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+3−3
@@ -575,13 +575,13 @@ putIndex i x arr =
575{-# INLINE putIndices #-}
575{-# INLINE putIndices #-}
576putIndices :: forall m a. (MonadIO m, Storable a)
576putIndices :: forall m a. (MonadIO m, Storable a)
577 => Array a -> Fold m (Int, a) ()
577 => Array a -> Fold m (Int, a) ()
578−putIndices Array{..} = FL.mkFoldM step initial extract
578+putIndices Array{..} = FL.rmapM extract $ FL.foldlM' step initial
579
579
580 where
580 where
581
581
582− initial = return $ FL.Partial ()
582+ initial = return ()
583
583
584− step () (i, x) = FL.Partial <$> liftIO (putIndexPtr arrStart aEnd i x)
584+ step () (i, x) = liftIO (putIndexPtr arrStart aEnd i x)
585
585
586 extract () = liftIO $ touch arrContents
586 extract () = liftIO $ touch arrContents
587
587
core/src/Streamly/Internal/Data/Fold.hs+11−13
@@ -31,12 +31,10 @@ module Streamly.Internal.Data.Fold
31 , foldl'
31 , foldl'
32 , foldlM'
32 , foldlM'
33 , foldl1'
33 , foldl1'
34+ , foldt'
35+ , foldtM'
34 , foldr
36 , foldr
35 , foldrM
37 , foldrM
36− , mkFold
37− , mkFold_
38− , mkFoldM
39− , mkFoldM_
40
38
41 -- * Folds
39 -- * Folds
42 -- ** Identity
40 -- ** Identity
@@ -700,7 +698,7 @@ sum = foldl' (+) 0
700-- /Since 0.8.0 (Added 'Eq' constraint)/
698-- /Since 0.8.0 (Added 'Eq' constraint)/
701{-# INLINE product #-}
699{-# INLINE product #-}
702product :: (Monad m, Num a, Eq a) => Fold m a a
700product :: (Monad m, Num a, Eq a) => Fold m a a
703−product = mkFold_ step (Partial 1)
701+product = foldt' step (Partial 1) id
704
702
705 where
703 where
706
704
@@ -1005,7 +1003,7 @@ drainN n = take n drain
1005-- /Pre-release/
1003-- /Pre-release/
1006{-# INLINE genericIndex #-}
1004{-# INLINE genericIndex #-}
1007genericIndex :: (Integral i, Monad m) => i -> Fold m a (Maybe a)
1005genericIndex :: (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)
1009
1007
1010 where
1008 where
1011
1009
@@ -1029,7 +1027,7 @@ index = genericIndex
1029--
1027--
1030{-# INLINE maybe #-}
1028{-# INLINE maybe #-}
1031maybe :: Monad m => (a -> Maybe b) -> Fold m a (Maybe b)
1029maybe :: 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
1033
1031
1034-- Naming notes:
1032-- Naming notes:
1035--
1033--
@@ -1079,7 +1077,7 @@ head = one
1079-- @since 0.7.0
1077-- @since 0.7.0
1080{-# INLINE find #-}
1078{-# INLINE find #-}
1081find :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
1079find :: 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)
1083
1081
1084 where
1082 where
1085
1083
@@ -1096,7 +1094,7 @@ find predicate = mkFold step (Partial ()) (const Nothing)
1096-- @since 0.7.0
1094-- @since 0.7.0
1097{-# INLINE lookup #-}
1095{-# INLINE lookup #-}
1098lookup :: (Eq a, Monad m) => a -> Fold m (a,b) (Maybe b)
1096lookup :: (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)
1100
1098
1101 where
1099 where
1102
1100
@@ -1110,7 +1108,7 @@ lookup a0 = mkFold step (Partial ()) (const Nothing)
1110-- @since 0.7.0
1108-- @since 0.7.0
1111{-# INLINE findIndex #-}
1109{-# INLINE findIndex #-}
1112findIndex :: Monad m => (a -> Bool) -> Fold m a (Maybe Int)
1110findIndex :: 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)
1114
1112
1115 where
1113 where
1116
1114
@@ -1156,7 +1154,7 @@ elemIndex a = findIndex (a ==)
1156-- @since 0.7.0
1154-- @since 0.7.0
1157{-# INLINE null #-}
1155{-# INLINE null #-}
1158null :: Monad m => Fold m a Bool
1156null :: Monad m => Fold m a Bool
1159−null = mkFold (\() _ -> Done False) (Partial ()) (const True)
1157+null = foldt' (\() _ -> Done False) (Partial ()) (const True)
1160
1158
1161-- | Returns 'True' if any of the elements of a stream satisfies a predicate.
1159-- | Returns 'True' if any of the elements of a stream satisfies a predicate.
1162--
1160--
@@ -1168,7 +1166,7 @@ null = mkFold (\() _ -> Done False) (Partial ()) (const True)
1168-- @since 0.7.0
1166-- @since 0.7.0
1169{-# INLINE any #-}
1167{-# INLINE any #-}
1170any :: Monad m => (a -> Bool) -> Fold m a Bool
1168any :: Monad m => (a -> Bool) -> Fold m a Bool
1171−any predicate = mkFold_ step initial
1169+any predicate = foldt' step initial id
1172
1170
1173 where
1171 where
1174
1172
@@ -1198,7 +1196,7 @@ elem a = any (a ==)
1198-- @since 0.7.0
1196-- @since 0.7.0
1199{-# INLINE all #-}
1197{-# INLINE all #-}
1200all :: Monad m => (a -> Bool) -> Fold m a Bool
1198all :: Monad m => (a -> Bool) -> Fold m a Bool
1201−all predicate = mkFold_ step initial
1199+all predicate = foldt' step initial id
1202
1200
1203 where
1201 where
1204
1202
core/src/Streamly/Internal/Data/Fold/Type.hs+8−36
@@ -327,12 +327,10 @@ module Streamly.Internal.Data.Fold.Type
327 , foldl'
327 , foldl'
328 , foldlM'
328 , foldlM'
329 , foldl1'
329 , foldl1'
330+ , foldt'
331+ , foldtM'
330 , foldr
332 , foldr
331 , foldrM
333 , foldrM
332− , mkFold
333− , mkFold_
334− , mkFoldM
335− , mkFoldM_
336
334
337 -- * Folds
335 -- * Folds
338 , fromPure
336 , fromPure
@@ -597,24 +595,11 @@ foldrM g z =
597--
595--
598-- /Pre-release/
596-- /Pre-release/
599--
597--
600−{-# INLINE mkFold #-}
598+{-# INLINE foldt' #-}
601−mkFold :: Monad m => (s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
599+foldt' :: Monad m => (s -> a -> Step s b) -> Step s b -> (s -> b) -> Fold m a b
602−mkFold step initial extract =
600+foldt' step initial extract =
603 Fold (\s a -> return $ step s a) (return initial) (return . extract)
601 Fold (\s a -> return $ step s a) (return initial) (return . extract)
604
602
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−
618-- | Make a terminating fold with an effectful step function and initial state,
603-- | Make a terminating fold with an effectful step function and initial state,
619-- and a state extraction function.
604-- and a state extraction function.
620--
605--
@@ -624,22 +609,9 @@ mkFold_ step initial = mkFold step initial id
624--
609--
625-- /Pre-release/
610-- /Pre-release/
626--
611--
627−{-# INLINE mkFoldM #-}
612+{-# INLINE foldtM' #-}
628−mkFoldM :: (s -> a -> m (Step s b)) -> m (Step s b) -> (s -> m b) -> Fold m a b
613+foldtM' :: (s -> a -> m (Step s b)) -> m (Step s b) -> (s -> m b) -> Fold m a b
629−mkFoldM = Fold
614+foldtM' = 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
643
615
644------------------------------------------------------------------------------
616------------------------------------------------------------------------------
645-- Refold
617-- Refold