mirror of
https://github.com/composewell/streamly.git
synced 2024-09-20 07:58:27 +03:00
Fix take, takeEQ, takeGE, sliceSepByMax.
- `take`, `takeEQ`, `takeGE` now work when the count is <0. - `sliceSepByMax` behaviour is now more in line with what the documentaton says in `Streamly.Internal.Data.Parser`.
This commit is contained in:
parent
1100c23999
commit
30823452a4
@ -381,6 +381,9 @@ satisfy = D.toParserK . D.satisfy
|
||||
-- >>> S.parse (PR.take 1 FL.toList) $ S.fromList [1]
|
||||
-- [1]
|
||||
--
|
||||
-- >>> S.parse (PR.take (-1) FL.toList) $ S.fromList [1]
|
||||
-- []
|
||||
--
|
||||
-- @
|
||||
-- S.chunksOf n f = S.splitParse (FL.take n f)
|
||||
-- @
|
||||
@ -515,6 +518,13 @@ sliceBeginWith = undefined
|
||||
-- hybrid of 'splitOn' and 'take'. The element on which the condition succeeds
|
||||
-- is dropped.
|
||||
--
|
||||
-- >>> even n = n `mod` 2 == 0
|
||||
-- >>> S.parse (PR.many FL.toList (PR.sliceSepByMax (==1) 5 FL.toList)) $ S.fromList [1..10]
|
||||
-- > [[],[2,3,4,5,6],[7,8,9,10]]
|
||||
--
|
||||
-- >>> S.parse (PR.many FL.toList (PR.sliceSepByMax even 10 FL.toList)) $ S.fromList [0..10]
|
||||
-- > [[],[1],[3],[5],[7],[9],[]]
|
||||
--
|
||||
-- /Internal/
|
||||
--
|
||||
{-# INLINABLE sliceSepByMax #-}
|
||||
|
@ -316,13 +316,15 @@ take n (Fold fstep finitial fextract) = Parser step initial extract
|
||||
|
||||
initial = Tuple' 0 <$> finitial
|
||||
|
||||
step (Tuple' i r) a = do
|
||||
res <- fstep r a
|
||||
let i1 = i + 1
|
||||
s1 = Tuple' i1 res
|
||||
if i1 < n
|
||||
then return $ Partial 0 s1
|
||||
else Done 0 <$> fextract res
|
||||
step (Tuple' i r) a
|
||||
| i < n = do
|
||||
res <- fstep r a
|
||||
let i1 = i + 1
|
||||
s1 = Tuple' i1 res
|
||||
if i1 < n
|
||||
then return $ Partial 0 s1
|
||||
else Done 0 <$> fextract res
|
||||
| otherwise = Done 1 <$> fextract r
|
||||
|
||||
extract (Tuple' _ r) = fextract r
|
||||
|
||||
@ -338,11 +340,15 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
|
||||
|
||||
initial = Tuple' 0 <$> finitial
|
||||
|
||||
step (Tuple' i r) a = do
|
||||
res <- fstep r a
|
||||
let i1 = i + 1
|
||||
s1 = Tuple' i1 res
|
||||
if i1 < n then return (Continue 0 s1) else Done 0 <$> fextract res
|
||||
step (Tuple' i r) a
|
||||
| i < n = do
|
||||
res <- fstep r a
|
||||
let i1 = i + 1
|
||||
s1 = Tuple' i1 res
|
||||
if i1 < n
|
||||
then return (Continue 0 s1)
|
||||
else Done 0 <$> fextract res
|
||||
| otherwise = Done 1 <$> fextract r
|
||||
|
||||
extract (Tuple' i r) =
|
||||
if n == i
|
||||
@ -361,10 +367,11 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
|
||||
--
|
||||
{-# INLINE takeGE #-}
|
||||
takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
|
||||
takeGE n (Fold fstep finitial fextract) = Parser step initial extract
|
||||
|
||||
takeGE cnt (Fold fstep finitial fextract) = Parser step initial extract
|
||||
where
|
||||
|
||||
n = max cnt 0
|
||||
|
||||
initial = Tuple' 0 <$> finitial
|
||||
|
||||
step (Tuple' i r) a = do
|
||||
@ -489,17 +496,18 @@ sliceSepByMax predicate cnt (Fold fstep finitial fextract) =
|
||||
where
|
||||
|
||||
initial = Tuple' 0 <$> finitial
|
||||
|
||||
step (Tuple' i r) a
|
||||
| not (predicate a) = do
|
||||
res <- fstep r a
|
||||
let i1 = i + 1
|
||||
s1 = Tuple' i1 res
|
||||
if i1 < cnt
|
||||
then return $ Partial 0 s1
|
||||
else do
|
||||
b <- fextract res
|
||||
return $ Done 0 b
|
||||
| not (predicate a) =
|
||||
if i < cnt
|
||||
then do
|
||||
res <- fstep r a
|
||||
let i1 = i + 1
|
||||
s1 = Tuple' i1 res
|
||||
return $ Partial 0 s1
|
||||
else Done 1 <$> fextract r
|
||||
| otherwise = Done 0 <$> fextract r
|
||||
|
||||
extract (Tuple' _ r) = fextract r
|
||||
|
||||
-- | See 'Streamly.Internal.Data.Parser.wordBy'.
|
||||
|
Loading…
Reference in New Issue
Block a user