on master help sign in
Rename: ltakeWhile -> takeSepBy & drainWhile -> drainSepBy
commit0032f35e9608a2157067ee11730795ef7d3834e1 authoradithyaov <adithya@composewell.com> dateTue 24 Nov 2020, 03:35 (6 years ago) parent8f7c0dd
3 files changed+26−11
src/Streamly/Internal/Data/Fold.hs+5−5 src/Streamly/Internal/Data/Fold/Types.hs+4−5 src/Streamly/Internal/Data/Parser/ParserD.hs+17−1
src/Streamly/Internal/Data/Fold.hs+5−5
@@ -70,7 +70,7 @@ module Streamly.Internal.Data.Fold
7070
7171 -- ** Partial Folds
7272 , drainN
73− , drainWhile
73+ , drainSepBy
7474 -- , lastN
7575 -- , (!!)
7676 -- , genericIndex
@@ -131,7 +131,7 @@ module Streamly.Internal.Data.Fold
131131 -- ** Trimming
132132 , ltake
133133 -- , lrunFor -- time
134− , ltakeWhile
134+ , takeSepBy
135135 {-
136136 , ltakeWhileM
137137 , ldrop
@@ -818,9 +818,9 @@ drainN n = ltake n drain
818818
819819-- | A fold that drains elements of its input as long as the predicate succeeds,
820820-- running the effects and discarding the results.
821−{-# INLINABLE drainWhile #-}
822−drainWhile :: Monad m => (a -> Bool) -> Fold m a ()
823−drainWhile p = ltakeWhile p drain
821+{-# INLINABLE drainSepBy #-}
822+drainSepBy :: Monad m => (a -> Bool) -> Fold m a ()
823+drainSepBy p = takeSepBy p drain
824824
825825------------------------------------------------------------------------------
826826-- To Elements
src/Streamly/Internal/Data/Fold/Types.hs+4−5
@@ -132,7 +132,7 @@ module Streamly.Internal.Data.Fold.Types
132132 , lfilterM
133133 , lcatMaybes
134134 , ltake
135− , ltakeWhile
135+ , takeSepBy
136136
137137 , teeWith
138138 , teeWithFst
@@ -587,10 +587,9 @@ ltake n (Fold fstep finitial fextract) = Fold step initial extract
587587-- | Takes elements from the input as long as the predicate succeeds.
588588--
589589-- @since 0.7.0
590−{-# INLINE ltakeWhile #-}
591−ltakeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
592−ltakeWhile predicate (Fold fstep finitial fextract) =
593− Fold step finitial fextract
590+{-# INLINE takeSepBy #-}
591+takeSepBy :: Monad m => (a -> Bool) -> Fold m a b -> Fold m a b
592+takeSepBy predicate (Fold fstep finitial fextract) = Fold step finitial fextract
594593
595594 where
596595
src/Streamly/Internal/Data/Parser/ParserD.hs+17−1
@@ -384,7 +384,23 @@ takeGE cnt (Fold fstep finitial fextract) = Parser step initial extract
384384--
385385{-# INLINE takeWhile #-}
386386takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
387−takeWhile predicate fld = fromFold (FL.ltakeWhile predicate fld)
387+takeWhile predicate (Fold fstep finitial fextract) =
388+ Parser step initial fextract
389+
390+ where
391+
392+ initial = finitial
393+
394+ step s a =
395+ if predicate a
396+ then do
397+ fres <- fstep s a
398+ return
399+ $ case fres of
400+ FL.Partial s1 -> Partial 0 s1
401+ FL.Done b -> Done 0 b
402+ else Done 1 <$> fextract s
403+
388404
389405-- | See 'Streamly.Internal.Data.Parser.takeWhile1'.
390406--