Partially Apply Arguments to a Function

Usage

partially_apply(f, ..., set_as_defaults = FALSE)

Arguments

f
A function
...
Named arguments to set
set_as_defaults
Should values in ... be set as defaults?

Partially Apply Arguments to a Function

Description

Partially apply arguments to a function, reducing the number of arguments it takes, or setting them as defaults.

Details

There are two strategies. In the default, with set_as_defaults=FALSE, we assign all supplied values into a new environment that is placed ahead of the function's native environment: the supplied values will be found first. The returned function will have an argument list that omits the given arguments.

In the second, with set_as_defaults=TRUE, we set the supplied values as defaults in the function's argument list. The returned function's argument list still includes the given arguments, but they are all moved after the unset values, and kept in the order provided. If the function contains ... as an argument, then these values will be put after the ellipsis, which means partial name matching won't work (don't use it anyway!).

Both strategies seem to offer similar performance. I suspect that the set_as_defaults=TRUE strategy is a little more fragile. They differ mainly in how the function is intended to be used - if you want to forever specify particular values for a value, use set_as_defaults=FALSE. If you want to generate new function that you might sometimes want to use different values in, use set_as_defaults=TRUE.

Author

Rich FitzJohn