on master help sign in
Rename foldMany and foldMany1

foldMany1 is the default foldMany now. foldMany is renamed to foldManyPost which is of limited use to split in an infix manner.

commit11831bed44954f5fbe251b1de3e8ea4363f2dff1 authorHarendra Kumar <harendra@composewell.com> dateMon 8 Feb 2021, 18:26 (6 years ago) parentf6522bd
4 files changed+89−98
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+14−14 benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+2−2 src/Streamly/Internal/Data/Stream/IsStream/Nesting.hs+18−20 src/Streamly/Internal/Data/Stream/StreamD/Type.hs+55−62
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+14−14
@@ -49,7 +49,7 @@ import Prelude hiding (last, length)
4949import Streamly.Benchmark.Common.Handle
5050
5151#ifdef INSPECTION
52−import Streamly.Internal.Data.Stream.StreamD.Type (Step(..), FoldMany1)
52+import Streamly.Internal.Data.Stream.StreamD.Type (Step(..), FoldMany)
5353
5454import qualified Streamly.Internal.Data.Array.Foreign.Mut.Types as MA
5555import qualified Streamly.Internal.Data.Stream.StreamD as D
@@ -310,14 +310,14 @@ o_1_space_reduce_toBytes env =
310310chunksOfSum :: Int -> Handle -> IO Int
311311chunksOfSum n inh = S.length $ S.chunksOf n FL.sum (S.unfold FH.read inh)
312312
313+foldManyPostChunksOfSum :: Int -> Handle -> IO Int
314+foldManyPostChunksOfSum n inh =
315+ S.length $ IP.foldManyPost (FL.takeLE n FL.sum) (S.unfold FH.read inh)
316+
313317foldManyChunksOfSum :: Int -> Handle -> IO Int
314318foldManyChunksOfSum n inh =
315319 S.length $ IP.foldMany (FL.takeLE n FL.sum) (S.unfold FH.read inh)
316320
317−foldMany1ChunksOfSum :: Int -> Handle -> IO Int
318−foldMany1ChunksOfSum n inh =
319− S.length $ IP.foldMany1 (FL.takeLE n FL.sum) (S.unfold FH.read inh)
320−
321321parseManyChunksOfSum :: Int -> Handle -> IO Int
322322parseManyChunksOfSum n inh =
323323 S.length
@@ -336,7 +336,7 @@ chunksOf n inh =
336336#ifdef INSPECTION
337337inspect $ hasNoTypeClasses 'chunksOf
338338inspect $ 'chunksOf `hasNoType` ''Step
339−inspect $ 'chunksOf `hasNoType` ''FoldMany1
339+inspect $ 'chunksOf `hasNoType` ''FoldMany
340340inspect $ 'chunksOf `hasNoType` ''AT.ArrayUnsafe -- AT.writeNUnsafe
341341inspect $ 'chunksOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.concat
342342inspect $ 'chunksOf `hasNoType` ''MA.ReadUState -- FH.read/A.read
@@ -358,21 +358,21 @@ o_1_space_reduce_read_grouped env =
358358 -- XXX investigate why we need inline/noinline in these cases (GHC)
359359 -- Chunk using parsers
360360 , mkBench
361− ("S.foldMany (FL.take " ++ show (bigSize env) ++ " FL.sum)")
361+ ("S.foldManyPost (FL.take " ++ show (bigSize env) ++ " FL.sum)")
362362 env
363− $ \inh _ -> noinline foldManyChunksOfSum (bigSize env) inh
363+ $ \inh _ -> noinline foldManyPostChunksOfSum (bigSize env) inh
364364 , mkBench
365− "S.foldMany (FL.take 1 FL.sum)"
365+ "S.foldManyPost (FL.take 1 FL.sum)"
366366 env
367− $ \inh _ -> inline foldManyChunksOfSum 1 inh
367+ $ \inh _ -> inline foldManyPostChunksOfSum 1 inh
368368 , mkBench
369− ("S.foldMany1 (FL.take " ++ show (bigSize env) ++ " FL.sum)")
369+ ("S.foldMany (FL.take " ++ show (bigSize env) ++ " FL.sum)")
370370 env
371− $ \inh _ -> noinline foldMany1ChunksOfSum (bigSize env) inh
371+ $ \inh _ -> noinline foldManyChunksOfSum (bigSize env) inh
372372 , mkBench
373− "S.foldMany1 (FL.take 1 FL.sum)"
373+ "S.foldMany (FL.take 1 FL.sum)"
374374 env
375− $ \inh _ -> inline foldMany1ChunksOfSum 1 inh
375+ $ \inh _ -> inline foldManyChunksOfSum 1 inh
376376 , mkBench
377377 ("S.parseMany (FL.take " ++ show (bigSize env) ++ " FL.sum)")
378378 env
benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+2−2
@@ -112,7 +112,7 @@ inspect $ hasNoTypeClasses 'readFromBytesNull
112112inspect $ 'readFromBytesNull `hasNoType` ''Step
113113inspect $ 'readFromBytesNull `hasNoType` ''MAS.SpliceState
114114inspect $ 'readFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
115−inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany1
115+inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
116116#endif
117117
118118-- | Send the file contents ('defaultChunkSize') to /dev/null
@@ -126,7 +126,7 @@ inspect $ hasNoTypeClasses 'readWithBufferOfFromBytesNull
126126inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''Step
127127inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''MAS.SpliceState
128128inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''AT.ArrayUnsafe -- FH.fromBytes/S.arraysOf
129−inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''D.FoldMany1
129+inspect $ 'readWithBufferOfFromBytesNull `hasNoType` ''D.FoldMany
130130#endif
131131
132132-- | Send the chunk content ('defaultChunkSize') to /dev/null
src/Streamly/Internal/Data/Stream/IsStream/Nesting.hs+18−20
@@ -127,7 +127,7 @@ module Streamly.Internal.Data.Stream.IsStream.Nesting
127127 -- ** Folding
128128 -- | Apply folds on a stream.
129129 , foldMany
130− , foldMany1
130+ , foldManyPost
131131 , foldSequence
132132 , foldIterate
133133
@@ -903,26 +903,26 @@ iterateMapLeftsWith combine f = iterateMapWith combine (either f (const K.nil))
903903--
904904-- XXX We need takeGE/takeBetween to implement "some" using "many".
905905
906−-- | Like 'foldMany1' but appends empty fold output if the fold and stream
906+-- | Like 'foldMany' but appends empty fold output if the fold and stream
907907-- termination aligns:
908908--
909909-- >>> f = Fold.takeLE 2 Fold.sum
910−-- >>> Stream.toList $ Stream.foldMany f $ Stream.fromList []
910+-- >>> Stream.toList $ Stream.foldManyPost f $ Stream.fromList []
911911-- > [0]
912−-- >>> Stream.toList $ Stream.foldMany f $ Stream.fromList [1..9]
912+-- >>> Stream.toList $ Stream.foldManyPost f $ Stream.fromList [1..9]
913913-- > [3,7,11,15,9]
914−-- >>> Stream.toList $ Stream.foldMany f $ Stream.fromList [1..10]
914+-- >>> Stream.toList $ Stream.foldManyPost f $ Stream.fromList [1..10]
915915-- > [3,7,11,15,19,0]
916916--
917917-- /Internal/
918918--
919−{-# INLINE foldMany #-}
920−foldMany
919+{-# INLINE foldManyPost #-}
920+foldManyPost
921921 :: (IsStream t, Monad m)
922922 => Fold m a b
923923 -> t m a
924924 -> t m b
925−foldMany f m = D.fromStreamD $ D.foldMany f (D.toStreamD m)
925+foldManyPost f m = D.fromStreamD $ D.foldManyPost f (D.toStreamD m)
926926
927927-- | Apply a 'Fold' repeatedly on a stream and emit the fold outputs in the
928928-- output stream.
@@ -930,26 +930,26 @@ foldMany f m = D.fromStreamD $ D.foldMany f (D.toStreamD m)
930930-- To sum every two contiguous elements in a stream:
931931--
932932-- >>> f = Fold.takeLE 2 Fold.sum
933−-- >>> Stream.toList $ Stream.foldMany1 f $ Stream.fromList [1..10]
933+-- >>> Stream.toList $ Stream.foldMany f $ Stream.fromList [1..10]
934934-- > [3,7,11,15,19]
935935--
936936-- On an empty stream the output is empty:
937937--
938−-- >>> Stream.toList $ Stream.foldMany1 f $ Stream.fromList []
938+-- >>> Stream.toList $ Stream.foldMany f $ Stream.fromList []
939939-- > []
940940--
941−-- Note @foldMany1 (takeLE 0)@ would result in an infinite loop in a non-empty
941+-- Note @foldMany (takeLE 0)@ would result in an infinite loop in a non-empty
942942-- stream.
943943--
944944-- /Internal/
945945--
946−{-# INLINE foldMany1 #-}
947−foldMany1
946+{-# INLINE foldMany #-}
947+foldMany
948948 :: (IsStream t, Monad m)
949949 => Fold m a b
950950 -> t m a
951951 -> t m b
952−foldMany1 f m = D.fromStreamD $ D.foldMany1 f (D.toStreamD m)
952+foldMany f m = D.fromStreamD $ D.foldMany f (D.toStreamD m)
953953
954954-- | Apply a stream of folds to an input stream and emit the results in the
955955-- output stream.
@@ -1005,7 +1005,7 @@ foldIterate _f _i _m = undefined
10051005-- > ["hello\n","world"]
10061006--
10071007-- @
1008−-- foldMany1 f = parseMany (fromFold f)
1008+-- foldMany f = parseMany (fromFold f)
10091009-- @
10101010--
10111011-- Known Issues: When the parser fails there is no way to get the remaining
@@ -1289,7 +1289,7 @@ splitOn predicate f =
12891289 --
12901290 -- Since a suffix split fold can be easily expressed using a
12911291 -- non-backtracking fold, we use that.
1292− foldMany (FL.sliceSepBy predicate f)
1292+ foldManyPost (FL.sliceSepBy predicate f)
12931293
12941294-- | Split on a suffixed separator element, dropping the separator. The
12951295-- supplied 'Fold' is applied on the split segments.
@@ -1342,8 +1342,7 @@ splitOn predicate f =
13421342splitOnSuffix
13431343 :: (IsStream t, Monad m)
13441344 => (a -> Bool) -> Fold m a b -> t m a -> t m b
1345−splitOnSuffix predicate f m =
1346− D.fromStreamD $ D.foldMany1 (FL.sliceSepBy predicate f) (D.toStreamD m)
1345+splitOnSuffix predicate f = foldMany (FL.sliceSepBy predicate f)
13471346
13481347-- | Split on a prefixed separator element, dropping the separator. The
13491348-- supplied 'Fold' is applied on the split segments.
@@ -1456,8 +1455,7 @@ wordsBy predicate f m =
14561455splitWithSuffix
14571456 :: (IsStream t, Monad m)
14581457 => (a -> Bool) -> Fold m a b -> t m a -> t m b
1459−splitWithSuffix predicate f m =
1460− D.fromStreamD $ D.foldMany1 (FL.sliceEndWith predicate f) (D.toStreamD m)
1458+splitWithSuffix predicate f = foldMany (FL.sliceEndWith predicate f)
14611459
14621460------------------------------------------------------------------------------
14631461-- Splitting - on a delimiter sequence
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+55−62
@@ -78,9 +78,9 @@ module Streamly.Internal.Data.Stream.StreamD.Type
7878 , concatMap
7979 , concatMapM
8080 , FoldMany (..) -- for inspection testing
81− , FoldMany1 (..)
81+ , FoldManyPost (..)
8282 , foldMany
83− , foldMany1
83+ , foldManyPost
8484 , groupsOf2
8585 , chunksOf
8686 )
@@ -787,15 +787,14 @@ instance Monad m => Monad (Stream m) where
787787------------------------------------------------------------------------------
788788
789789-- s = stream state, fs = fold state
790−{-# ANN type FoldMany Fuse #-}
791−data FoldMany s fs b a
792− = FoldManyStart s
793− | FoldManyLoop s fs
794− | FoldManyYield b (FoldMany s fs b a)
795− | FoldManyDone
796−
797−-- | This is the stream equivalent of "Data.Fold.Internal.many". The fold
798−-- may consume 0 or more elements. It means:
790+{-# ANN type FoldManyPost Fuse #-}
791+data FoldManyPost s fs b a
792+ = FoldManyPostStart s
793+ | FoldManyPostLoop s fs
794+ | FoldManyPostYield b (FoldManyPost s fs b a)
795+ | FoldManyPostDone
796+
797+-- | Like foldMany but with the following differences:
799798--
800799-- * If the stream is empty the default value of the fold would still be
801800-- emitted in the output.
@@ -803,10 +802,10 @@ data FoldMany s fs b a
803802-- receive any input it would still yield the default fold accumulator as the
804803-- last value.
805804--
806−{-# INLINE_NORMAL foldMany #-}
807−foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
808−foldMany (Fold fstep initial extract) (Stream step state) =
809− Stream step' (FoldManyStart state)
805+{-# INLINE_NORMAL foldManyPost #-}
806+foldManyPost :: Monad m => Fold m a b -> Stream m a -> Stream m b
807+foldManyPost (Fold fstep initial extract) (Stream step state) =
808+ Stream step' (FoldManyPostStart state)
810809
811810 where
812811
@@ -816,56 +815,50 @@ foldMany (Fold fstep initial extract) (Stream step state) =
816815 return
817816 $ Skip
818817 $ case res of
819− FL.Done b -> FoldManyYield b (FoldManyStart s)
820− FL.Partial ps -> FoldManyLoop s ps
818+ FL.Done b -> FoldManyPostYield b (FoldManyPostStart s)
819+ FL.Partial ps -> FoldManyPostLoop s ps
821820
822821 {-# INLINE_LATE step' #-}
823− step' _ (FoldManyStart st) = do
822+ step' _ (FoldManyPostStart st) = do
824823 r <- initial
825824 return
826825 $ Skip
827826 $ case r of
828− FL.Done b -> FoldManyYield b (FoldManyStart st)
829− FL.Partial fs -> FoldManyLoop st fs
830− step' gst (FoldManyLoop st fs) = do
827+ FL.Done b -> FoldManyPostYield b (FoldManyPostStart st)
828+ FL.Partial fs -> FoldManyPostLoop st fs
829+ step' gst (FoldManyPostLoop st fs) = do
831830 r <- step (adaptState gst) st
832831 case r of
833832 Yield x s -> consume x s fs
834− Skip s -> return $ Skip (FoldManyLoop s fs)
833+ Skip s -> return $ Skip (FoldManyPostLoop s fs)
835834 Stop -> do
836835 b <- extract fs
837− return $ Skip (FoldManyYield b FoldManyDone)
838− step' _ (FoldManyYield b next) = return $ Yield b next
839− step' _ FoldManyDone = return Stop
836+ return $ Skip (FoldManyPostYield b FoldManyPostDone)
837+ step' _ (FoldManyPostYield b next) = return $ Yield b next
838+ step' _ FoldManyPostDone = return Stop
840839
841−{-# ANN type FoldMany1 Fuse #-}
842−data FoldMany1 s fs b a
843− = FoldMany1Start s
844− | FoldMany1First fs s
845− | FoldMany1Loop s fs
846− | FoldMany1Yield b (FoldMany1 s fs b a)
847− | FoldMany1Done
840+{-# ANN type FoldMany Fuse #-}
841+data FoldMany s fs b a
842+ = FoldManyStart s
843+ | FoldManyFirst fs s
844+ | FoldManyLoop s fs
845+ | FoldManyYield b (FoldMany s fs b a)
846+ | FoldManyDone
848847
849−-- | Like 'foldMany' except that the fold consumes 1 or more elements. It
850−-- means:
848+-- | Apply a fold multiple times until the stream ends. If the stream is empty
849+-- the output would be empty.
851850--
852−-- * If the stream is empty the output would be empty.
853−-- * At the end of the stream if the last application of the fold did not
854−-- receive any input it would not result in any output.
855−--
856−-- @foldMany1 f = parseMany (fromFold f)@
851+-- @foldMany f = parseMany (fromFold f)@
857852--
858−-- This could be problematic unless we use an accumulator fold instead of a
859−-- terminating fold. A terminating fold may terminate even without accepting a
860−-- single element. We can use parseMany instead where we can return the input
861−-- if the fold terminates without accepting any input. The alternative is to
862−-- always execute the fold's initial action and discard it in case the stream
863−-- stops without actually feeding input to it.
853+-- A terminating fold may terminate even without accepting a single input. So
854+-- we run the fold's initial action before evaluating the stream. However, this
855+-- means that if later the stream does not yield anything we have to discard
856+-- the fold's initial result which could have generated an effect.
864857--
865−{-# INLINE_NORMAL foldMany1 #-}
866−foldMany1 :: Monad m => Fold m a b -> Stream m a -> Stream m b
867−foldMany1 (Fold fstep initial extract) (Stream step state) =
868− Stream step' (FoldMany1Start state)
858+{-# INLINE_NORMAL foldMany #-}
859+foldMany :: Monad m => Fold m a b -> Stream m a -> Stream m b
860+foldMany (Fold fstep initial extract) (Stream step state) =
861+ Stream step' (FoldManyStart state)
869862
870863 where
871864
@@ -875,37 +868,37 @@ foldMany1 (Fold fstep initial extract) (Stream step state) =
875868 return
876869 $ Skip
877870 $ case res of
878− FL.Done b -> FoldMany1Yield b (FoldMany1Start s)
879− FL.Partial ps -> FoldMany1Loop s ps
871+ FL.Done b -> FoldManyYield b (FoldManyStart s)
872+ FL.Partial ps -> FoldManyLoop s ps
880873
881874 {-# INLINE_LATE step' #-}
882− step' _ (FoldMany1Start st) = do
875+ step' _ (FoldManyStart st) = do
883876 r <- initial
884877 return
885878 $ Skip
886879 $ case r of
887− FL.Done b -> FoldMany1Yield b (FoldMany1Start st)
888− FL.Partial fs -> FoldMany1First fs st
889− step' gst (FoldMany1First fs st) = do
880+ FL.Done b -> FoldManyYield b (FoldManyStart st)
881+ FL.Partial fs -> FoldManyFirst fs st
882+ step' gst (FoldManyFirst fs st) = do
890883 r <- step (adaptState gst) st
891884 case r of
892885 Yield x s -> consume x s fs
893− Skip s -> return $ Skip (FoldMany1First fs s)
886+ Skip s -> return $ Skip (FoldManyFirst fs s)
894887 Stop -> return Stop
895− step' gst (FoldMany1Loop st fs) = do
888+ step' gst (FoldManyLoop st fs) = do
896889 r <- step (adaptState gst) st
897890 case r of
898891 Yield x s -> consume x s fs
899− Skip s -> return $ Skip (FoldMany1Loop s fs)
892+ Skip s -> return $ Skip (FoldManyLoop s fs)
900893 Stop -> do
901894 b <- extract fs
902− return $ Skip (FoldMany1Yield b FoldMany1Done)
903− step' _ (FoldMany1Yield b next) = return $ Yield b next
904− step' _ FoldMany1Done = return Stop
895+ return $ Skip (FoldManyYield b FoldManyDone)
896+ step' _ (FoldManyYield b next) = return $ Yield b next
897+ step' _ FoldManyDone = return Stop
905898
906899{-# INLINE chunksOf #-}
907900chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
908−chunksOf n f = foldMany1 (FL.takeLE n f)
901+chunksOf n f = foldMany (FL.takeLE n f)
909902
910903data GroupState2 s fs
911904 = GroupStart2 s