I am pleased to announce the release of
curry – a small package I’ve developed
as part of improving my meta-programming skills.
curry is yet another attempt
at providing a native currying/partial application mechanism in R. Other
examples of implementations of this can be found in
functional (and probably
curry sets itself apart in the manner it is used and in the functions
curry is operator based and a partially applied function retains
named arguments for easier autocomplete etc.
curry provides three mechanisms
for partial application:
partial()), as well as a true currying operator (
%<!%) and a “weak partial
%<?%) - read on to see the differences.
Below I’ll show the different usages of the
Currying is the reduction of the arity of a function by fixing the first argument, returning a new function lacking this (no it’s not - read on or click here).
Tail currying is just like currying except it reduces the arity of the function from the other end by fixing the last argument.
Partial function application
When the argument you wish to fix is not in either end of the argument list it
is necessary to use a more generalised approach. Using
it is possible to fix any (and multiple) arguments in a function using a list of
values to fix.
Other efforts in this has the drawback of returning a new function with just an
ellipsis, making argument checks and autocomplete impossible. With
returned functions retains named arguments (minus the fixed ones).
The above uses a very loose (incorrect) definition of currying. The correct definition is that currying a function returns a function taking a single argument (the first from the original function). Calling a curried function will return a new function accepting the second argument of the original function and so on. Once all arguments of the original function has been consumed it evaluates the call and returns the result. Thus:
True currying is less useful in R as it does not play nice with function
... as the argument list will never be consumed. Still, it is
curry using the
Curry() function or the
As with the partial application functionality the strict currying retains argument names and defaults at each step of the currying sequence:
Weak partial function application
The last functionality provided by
curry is a “weak” partial function
application in the sense that it sets (or changes) argument defaults. Thus,
compared to partial application it returns a function with the same arguments,
but if the defaulted arguments are ignored it will be equivalent to a partial
application. Defaults can be set or changed using the
curry can be installed from CRAN:
but for the latest and greatest use GitHub:
curry adds a layer around the manipulated functions adding some overhead, but
the overhead is not accumulative (currying or partially applying multiple times
does not add additional overhead). depending on the runtime of the function the
overhead can seem large but as the complexity of the call increases, the effect
of the overhead will decrease: