Build a wrapped function that applies to each unique argument in a vector of arguments once.

VectorizeM(
  FUN,
  vectorize.args = arg.names,
  SIMPLIFY = TRUE,
  USE.NAMES = TRUE,
  UNLIST = FALSE
)

Arguments

FUN

function to apply

vectorize.args

a character vector of arguments which should be vectorized. Defaults to first argument of FUN. If set must be length 1.

SIMPLIFY

logical or character string; attempt to reduce the result to a vector, matrix or higher dimensional array; see the simplify argument of sapply.

USE.NAMES

logical; use names if the first ... argument has names, or if it is a character vector, use that character vector as the names.

UNLIST

logical; if TRUE try to unlist the result.

Value

adapted function (vectorized with one call per different value).

Details

Only sensible for pure side-effect free deterministic functions.

See also

Examples

fs <- function(x) { x <- x[[1]]; print(paste("see", x)); sin(x) } fv <- VectorizeM(fs) # should only print "see" twice, not 6 times fv(c(0, 1, 1, 0, 0, 1))
#> [1] "see 0" #> [1] "see 1"
#> [1] 0.000000 0.841471 0.841471 0.000000 0.000000 0.841471