Type: | Package |
Title: | Partial Function Application with %<%, %-<%, and %><% |
Version: | 0.1.1 |
Date: | 2016-09-28 |
Author: | Thomas Lin Pedersen |
Maintainer: | Thomas Lin Pedersen <thomasp85@gmail.com> |
Description: | Partial application is the process of reducing the arity of a function by fixing one or more arguments, thus creating a new function lacking the fixed arguments. The curry package provides three different ways of performing partial function application by fixing arguments from either end of the argument list (currying and tail currying) or by fixing multiple named arguments (partial application). This package provides this functionality through the %<%, %-<%, and %><% operators which allows for a programming style comparable to modern functional languages. Compared to other implementations such a purrr::partial() the operators in curry composes functions with named arguments, aiding in autocomplete etc. |
License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
LazyData: | TRUE |
RoxygenNote: | 5.0.1 |
Collate: | 'utils.R' 'curry.R' 'partial.R' 'scaffold.R' 'tail_curry.R' |
URL: | https://github.com/thomasp85/curry |
BugReports: | https://github.com/thomasp85/curry/issues |
NeedsCompilation: | no |
Packaged: | 2016-09-28 18:18:09 UTC; Thomas |
Repository: | CRAN |
Date/Publication: | 2016-09-28 22:18:23 |
Curry a function from the start
Description
The curry
function and the %<%
operator performs currying on
a function by partially applying the first argument, returning a function
that accepts all but the first arguments of the former function. If the first
argument is ...
the curried argument will be interpreted as part of
the ellipsis and the ellipsis will be retained in the returned function. It
is thus possible to curry functions comtaining ellipis arguments to infinity
(though not adviced).
Usage
fun %<% arg
curry(fun, arg)
Arguments
fun |
A function to be curried. Can be any function (normal, already curried, primitives). |
arg |
The value that should be applied to the first argument. |
Value
A function with the same arguments as fun
except for the
first, unless the first is ...
in which case it will be retained.
Note
Multiple currying does not result in multiple nested calls, so while the first currying adds a layer around the curried function, potentially adding a very small performance hit, currying multiple times will not add to this effect.
See Also
Other partials: partial
,
tail_curry
Examples
# Equivalent to curry(`+`, 5)
add_5 <- `+` %<% 5
add_5(10)
# ellipsis are retained when currying
bind_5 <- cbind %<% 5
bind_5(1:10)
Apply arguments partially to a function
Description
The partial
function and the %><%
operator allows you to
partially call a function with a list of arguments. Named elements in the
list will be matched to function arguments and these arguments will be
removed from the returned function. Unnamed elements are only allowed for
functions containing an ellipsis, in which case they are considered part of
the ellipsis.
Usage
fun %><% args
partial(fun, args)
Arguments
fun |
A function to be partially applied. Can be any function (normal, already partially applied, primitives). |
args |
A list of values that should be applied to the function. |
Value
A function with the same arguments as fun
except for the ones
given in args
Note
Multiple partial application does not result in multiple nested calls, so while the first partial call adds a layer around the called function, potentially adding a very small performance hit, partially calling multiple times will not add to this effect.
See Also
Other partials: curry
,
tail_curry
Examples
dummy_lengths <- vapply %><% list(FUN = length, FUN.VALUE = integer(1))
test_list <- list(a = 1:5, b = 1:10)
dummy_lengths(test_list)
Curry a function from the end
Description
The tail_curry
function and the %-<%
operator performs
currying on a function by partially applying the last argument, returning a
function that accepts all but the last arguments of the former function. If
the last argument is ...
the curried argument will be interpreted as
the last named argument. If the only argument to the function is ...
the curried argument will be interpreted as part of the ellipsis and the
ellipsis will be retained in the returned function. It is thus possible to
curry functions comtaining ellipis arguments to infinity (though not
adviced).
Usage
fun %-<% arg
tail_curry(fun, arg)
Arguments
fun |
A function to be curried from the end. Can be any function (normal, already (tail_)curried, primitives). |
arg |
The value that should be applied to the last argument. |
Value
A function with the same arguments as fun
except for the
last named argument, unless the only one is ...
in which case it will
be retained.
Note
Multiple tail_currying does not result in multiple nested calls, so while the first tail_currying adds a layer around the curried function, potentially adding a very small performance hit, tail_currying multiple times will not add to this effect.
See Also
Other partials: curry
, partial
Examples
# Equivalent to tail_curry(`/`, 5)
divide_by_5 <- `/` %-<% 5
divide_by_5(10)
no_factors <- data.frame %-<% FALSE
no_factors(x = letters[1:5])