takeWhile

takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.

Laziness

>>> takeWhile (const False) undefined
*** Exception: Prelude.undefined
>>> takeWhile (const False) (undefined : undefined)
[]
>>> take 1 (takeWhile (const True) (1 : undefined))
[1]

Examples

>>> takeWhile (< 3) [1,2,3,4,1,2,3,4]
[1,2]
>>> takeWhile (< 9) [1,2,3]
[1,2,3]
>>> takeWhile (< 0) [1,2,3]
[]
takeWhile p xs returns the longest prefix of the stream xs for which the predicate p holds.
Similar to takeWhile, returns the longest (possibly empty) prefix of elements satisfying the predicate.
takeWhile, applied to a predicate p and a ByteString xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.
Similar to takeWhile, returns the longest (possibly empty) prefix of elements satisfying the predicate.
O(n) takeWhile, applied to a predicate p and a Text, returns the longest prefix (possibly empty) of elements that satisfy p.
takeWhile, applied to a predicate p and a stream, returns the longest prefix (possibly empty) of elements that satisfy p. Properties
unstream . takeWhile p . stream = takeWhile p
Consume input as long as the predicate returns True, and return the consumed input. This parser does not fail. It will return an empty string if the predicate returns False on the first byte of input. Note: Because this parser does not fail, do not use it with combinators such as many, because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.
Consume input as long as the predicate returns True, and return the consumed input. This parser does not fail. It will return an empty string if the predicate returns False on the first byte of input. Note: Because this parser does not fail, do not use it with combinators such as many, because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.
Consume input as long as the predicate returns True, and return the consumed input. This parser does not fail. It will return an empty string if the predicate returns False on the first character of input. Note: Because this parser does not fail, do not use it with combinators such as many, because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.
Consume input while the predicate returns True.
Stream all values downstream that match the given predicate. Same caveats regarding downstream termination apply as with take.
takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.
>>> takeWhile (< 3) [1,2,3,4,1,2,3,4]
[1,2]

>>> takeWhile (< 9) [1,2,3]
[1,2,3]

>>> takeWhile (< 0) [1,2,3]
[]
Take bytes while the @predicate hold from the current position in the stream
Return all bytes while the predicate returns True. Since 0.3.0
Since 1.0.8
(takeWhile p) allows values to pass downstream so long as they satisfy the predicate p.
takeWhile (pure True) = cat

takeWhile (liftA2 (&&) p1 p2) = takeWhile p1 >-> takeWhile p2
Alternative version of reverse . takeWhile p . reverse.
forAllPredicates $ \p xs -> Rev.takeWhile p xs == reverse (List.takeWhile p (reverse xs))
forAllPredicates $ \p xs -> Rev.takeWhile p xs == reverse (List.takeWhile p (reverse xs))
\x xs pad -> defined $ Rev.takeWhile ((x::Char)/=) $ Match.replicate (pad::[()]) undefined ++ x:xs
takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.
>>> takeWhile (< 3) [1,2,3,4,1,2,3,4]
[1,2]

>>> takeWhile (< 9) [1,2,3]
[1,2,3]

>>> takeWhile (< 0) [1,2,3]
[]
Laziness:
>>> takeWhile (const False) undefined
*** Exception: Prelude.undefined

>>> takeWhile (const False) (undefined : undefined)
[]

>>> take 1 (takeWhile (const True) (1 : undefined))
[1]
End stream when an element fails a condition; the original return value is lost. By contrast span preserves this information, and is generally more desirable.
S.takeWhile thus = void . S.span thus
To preserve the information - but thus also force the rest of the stream to be developed - write
S.drained . S.span thus
as dropWhile thus is
S.effects . S.span thus
takeWhile, applied to a predicate p and a ByteString xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p.
takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p:
takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2]
takeWhile (< 9) [1,2,3] == [1,2,3]
takeWhile (< 0) [1,2,3] == []
O(n) takeWhile, applied to a predicate p and a Text, returns the longest prefix (possibly empty) of elements that satisfy p. Subject to fusion.