Skip to content

Which higher-order function

1 message · Hadley Wickham

#
Would it be appropriate to add a Which higher-order function?

Which <- function(f, x) {
   ind <- vapply(x, f, logical(1))
   !is.na(ind) & ind
}

Then Filter would be:

Filter <- function(f, x) x[Which(f, x)]

I think the use of vapply is slightly better than the current
as.logical(sapply(x, f)) because it will return a more informative
error message if the predicate doesn't return a logical vector of the
correct length.

Hadley