Add takeWhile1

This commit is contained in:
Harendra Kumar 2020-04-09 13:44:12 +05:30
parent 5f4de8630c
commit 3622e37b4e

View File

@ -67,6 +67,7 @@ module Streamly.Internal.Data.Parser
-- Grab a sequence of input elements by inspecting them
, takeWhile
-- , takeWhileBetween
, takeWhile1
, sepBy
, sepByMax
-- , sepByBetween
@ -400,6 +401,38 @@ takeWhile predicate (Fold fstep finitial fextract) =
then Yield 0 <$> fstep s a
else Stop 1 <$> fextract s
-- | Like 'takeWhile' but takes at least one element otherwise fails.
--
-- /Internal/
--
{-# INLINE takeWhile1 #-}
takeWhile1 :: MonadThrow m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile1 predicate (Fold fstep finitial fextract) =
Parser step initial extract
where
initial = return Nothing
step Nothing a =
if predicate a
then do
s <- finitial
r <- fstep s a
return $ Yield 0 (Just r)
else return $ Error "takeWhile1: empty"
step (Just s) a =
if predicate a
then do
r <- fstep s a
return $ Yield 0 (Just r)
else do
b <- fextract s
return $ Stop 1 b
extract Nothing = throwM $ ParseError "takeWhile1: end of input"
extract (Just s) = fextract s
-- | Collect stream elements until an element succeeds the predicate. Drop the
-- element on which the predicate succeeded. The succeeding element is treated
-- as an infix separator which is dropped from the output.