on master help sign in
Fix take, takeEQ, takeGE, sliceSepByMax.
3082345pranaysashank
src/Streamly/Internal/Data/Parser.hs+10
@@ -381,6 +381,9 @@ satisfy = D.toParserK . D.satisfy
381-- >>> S.parse (PR.take 1 FL.toList) $ S.fromList [1]
381-- >>> S.parse (PR.take 1 FL.toList) $ S.fromList [1]
382-- [1]
382-- [1]
383--
383--
384+-- >>> S.parse (PR.take (-1) FL.toList) $ S.fromList [1]
385+-- []
386+--
384-- @
387-- @
385-- S.chunksOf n f = S.splitParse (FL.take n f)
388-- S.chunksOf n f = S.splitParse (FL.take n f)
386-- @
389-- @
@@ -515,6 +518,13 @@ sliceBeginWith = undefined
515-- hybrid of 'splitOn' and 'take'. The element on which the condition succeeds
518-- hybrid of 'splitOn' and 'take'. The element on which the condition succeeds
516-- is dropped.
519-- is dropped.
517--
520--
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+--
518-- /Internal/
528-- /Internal/
519--
529--
520{-# INLINABLE sliceSepByMax #-}
530{-# 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
316
316
317 initial = Tuple' 0 <$> finitial
317 initial = Tuple' 0 <$> finitial
318
318
319− step (Tuple' i r) a = do
319+ step (Tuple' i r) a
320+ | i < n = do
320− res <- fstep r a
321+ res <- fstep r a
321− let i1 = i + 1
322+ let i1 = i + 1
322− s1 = Tuple' i1 res
323+ s1 = Tuple' i1 res
323− if i1 < n
324+ if i1 < n
324− then return $ Partial 0 s1
325+ then return $ Partial 0 s1
325− else Done 0 <$> fextract res
326+ else Done 0 <$> fextract res
327+ | otherwise = Done 1 <$> fextract r
326
328
327 extract (Tuple' _ r) = fextract r
329 extract (Tuple' _ r) = fextract r
328
330
@@ -338,11 +340,15 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
338
340
339 initial = Tuple' 0 <$> finitial
341 initial = Tuple' 0 <$> finitial
340
342
341− step (Tuple' i r) a = do
343+ step (Tuple' i r) a
344+ | i < n = do
342− res <- fstep r a
345+ res <- fstep r a
343− let i1 = i + 1
346+ let i1 = i + 1
344− s1 = Tuple' i1 res
347+ s1 = Tuple' i1 res
348+ if i1 < n
345− if i1 < n then return (Continue 0 s1) else Done 0 <$> fextract res
349+ then return (Continue 0 s1)
350+ else Done 0 <$> fextract res
351+ | otherwise = Done 1 <$> fextract r
346
352
347 extract (Tuple' i r) =
353 extract (Tuple' i r) =
348 if n == i
354 if n == i
@@ -361,10 +367,11 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
361--
367--
362{-# INLINE takeGE #-}
368{-# INLINE takeGE #-}
363takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
369takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
364−takeGE n (Fold fstep finitial fextract) = Parser step initial extract
370+takeGE cnt (Fold fstep finitial fextract) = Parser step initial extract
365−
366 where
371 where
367
372
373+ n = max cnt 0
374+
368 initial = Tuple' 0 <$> finitial
375 initial = Tuple' 0 <$> finitial
369
376
370 step (Tuple' i r) a = do
377 step (Tuple' i r) a = do
@@ -489,17 +496,18 @@ sliceSepByMax predicate cnt (Fold fstep finitial fextract) =
489 where
496 where
490
497
491 initial = Tuple' 0 <$> finitial
498 initial = Tuple' 0 <$> finitial
499+
492 step (Tuple' i r) a
500 step (Tuple' i r) a
493− | not (predicate a) = do
501+ | not (predicate a) =
502+ if i < cnt
503+ then do
494− res <- fstep r a
504+ res <- fstep r a
495− let i1 = i + 1
505+ let i1 = i + 1
496− s1 = Tuple' i1 res
506+ s1 = Tuple' i1 res
497− if i1 < cnt
498− then return $ Partial 0 s1
507+ return $ Partial 0 s1
499− else do
500− b <- fextract res
508+ else Done 1 <$> fextract r
501− return $ Done 0 b
502 | otherwise = Done 0 <$> fextract r
509 | otherwise = Done 0 <$> fextract r
510+
503 extract (Tuple' _ r) = fextract r
511 extract (Tuple' _ r) = fextract r
504
512
505-- | See 'Streamly.Internal.Data.Parser.wordBy'.
513-- | See 'Streamly.Internal.Data.Parser.wordBy'.