head_while: Find head/tail that all satisfies a predicate.

View source: R/head-tail.R

head_whileR Documentation

Find head/tail that all satisfies a predicate.


Find head/tail that all satisfies a predicate.


head_while(.x, .p, ...)

tail_while(.x, .p, ...)



A list or atomic vector.


A single predicate function, a formula describing such a predicate function, or a logical vector of the same length as .x. Alternatively, if the elements of .x are themselves lists of objects, a string indicating the name of a logical element in the inner lists. Only those elements where .p evaluates to TRUE will be modified.


Additional arguments passed on to the mapped function.

We now generally recommend against using ... to pass additional (constant) arguments to .f. Instead use a shorthand anonymous function:

# Instead of
x |> map(f, 1, 2, collapse = ",")
# do:
x |> map(\(x) f(x, 1, 2, collapse = ","))

This makes it easier to understand which arguments belong to which function and will tend to yield better error messages.


A vector the same type as .x.


pos <- function(x) x >= 0
head_while(5:-5, pos)
tail_while(5:-5, negate(pos))

big <- function(x) x > 100
head_while(0:10, big)
tail_while(0:10, big)

purrr documentation built on Aug. 10, 2023, 9:08 a.m.