on master help sign in
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`.

commit30823452a446e1956a65597290d14d07341757e1 authorpranaysashank <pranaysashank@composewell.com> dateSun 28 Jun 2020, 21:33 (6 years ago) parent1100c23
2 files changed+41−23
src/Streamly/Internal/Data/Parser.hs+10 src/Streamly/Internal/Data/Parser/ParserD.hs+31−23
src/Streamly/Internal/Data/Parser.hs+10
@@ -381,6 +381,9 @@ satisfy = D.toParserK . D.satisfy
381381-- >>> S.parse (PR.take 1 FL.toList) $ S.fromList [1]
382382-- [1]
383383--
384+-- >>> S.parse (PR.take (-1) FL.toList) $ S.fromList [1]
385+-- []
386+--
384387-- @
385388-- S.chunksOf n f = S.splitParse (FL.take n f)
386389-- @
@@ -515,6 +518,13 @@ sliceBeginWith = undefined
515518-- hybrid of 'splitOn' and 'take'. The element on which the condition succeeds
516519-- is dropped.
517520--
521+-- >>> even n = n `mod` 2 == 0
522+-- >>> S.parse (PR.many FL.toList (PR.sliceSepByMax (==1) 5 FL.toList)) $ S.fromList [1..10]
523+-- > [[],[2,3,4,5,6],[7,8,9,10]]
524+--
525+-- >>> S.parse (PR.many FL.toList (PR.sliceSepByMax even 10 FL.toList)) $ S.fromList [0..10]
526+-- > [[],[1],[3],[5],[7],[9],[]]
527+--
518528-- /Internal/
519529--
520530{-# INLINABLE sliceSepByMax #-}
src/Streamly/Internal/Data/Parser/ParserD.hs+31−23
@@ -316,13 +316,15 @@ take n (Fold fstep finitial fextract) = Parser step initial extract
316316
317317 initial = Tuple' 0 <$> finitial
318318
319− step (Tuple' i r) a = do
320− res <- fstep r a
321− let i1 = i + 1
322− s1 = Tuple' i1 res
323− if i1 < n
324− then return $ Partial 0 s1
325− else Done 0 <$> fextract res
319+ step (Tuple' i r) a
320+ | i < n = do
321+ res <- fstep r a
322+ let i1 = i + 1
323+ s1 = Tuple' i1 res
324+ if i1 < n
325+ then return $ Partial 0 s1
326+ else Done 0 <$> fextract res
327+ | otherwise = Done 1 <$> fextract r
326328
327329 extract (Tuple' _ r) = fextract r
328330
@@ -338,11 +340,15 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
338340
339341 initial = Tuple' 0 <$> finitial
340342
341− step (Tuple' i r) a = do
342− res <- fstep r a
343− let i1 = i + 1
344− s1 = Tuple' i1 res
345− if i1 < n then return (Continue 0 s1) else Done 0 <$> fextract res
343+ step (Tuple' i r) a
344+ | i < n = do
345+ res <- fstep r a
346+ let i1 = i + 1
347+ s1 = Tuple' i1 res
348+ if i1 < n
349+ then return (Continue 0 s1)
350+ else Done 0 <$> fextract res
351+ | otherwise = Done 1 <$> fextract r
346352
347353 extract (Tuple' i r) =
348354 if n == i
@@ -361,10 +367,11 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
361367--
362368{-# INLINE takeGE #-}
363369takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
364−takeGE n (Fold fstep finitial fextract) = Parser step initial extract
365−
370+takeGE cnt (Fold fstep finitial fextract) = Parser step initial extract
366371 where
367372
373+ n = max cnt 0
374+
368375 initial = Tuple' 0 <$> finitial
369376
370377 step (Tuple' i r) a = do
@@ -489,17 +496,18 @@ sliceSepByMax predicate cnt (Fold fstep finitial fextract) =
489496 where
490497
491498 initial = Tuple' 0 <$> finitial
499+
492500 step (Tuple' i r) a
493− | not (predicate a) = do
494− res <- fstep r a
495− let i1 = i + 1
496− s1 = Tuple' i1 res
497− if i1 < cnt
498− then return $ Partial 0 s1
499− else do
500− b <- fextract res
501− return $ Done 0 b
501+ | not (predicate a) =
502+ if i < cnt
503+ then do
504+ res <- fstep r a
505+ let i1 = i + 1
506+ s1 = Tuple' i1 res
507+ return $ Partial 0 s1
508+ else Done 1 <$> fextract r
502509 | otherwise = Done 0 <$> fextract r
510+
503511 extract (Tuple' _ r) = fextract r
504512
505513-- | See 'Streamly.Internal.Data.Parser.wordBy'.