Find head/tail that all satisfies a predicate.
Arguments
- .x
A list or atomic vector.
- .p
A predicate function specified in one of the following ways:
A named function, e.g.
is.character.An anonymous function, e.g.
\(x) all(x < 0)orfunction(x) all(x < 0).A logical vector of the same length as
.x.A string, if the elements of
.xare themselves lists of objects, giving the name of a logical element in the inner lists.
Only those elements where
.pevaluates toTRUEwill be kept.- ...
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:This makes it easier to understand which arguments belong to which function and will tend to yield better error messages.
Examples
pos <- function(x) x >= 0
head_while(5:-5, pos)
#> [1] 5 4 3 2 1 0
tail_while(5:-5, negate(pos))
#> [1] -1 -2 -3 -4 -5
big <- function(x) x > 100
head_while(0:10, big)
#> integer(0)
tail_while(0:10, big)
#> integer(0)
