Title: | Curry, Compose, and other higher-order functions |
---|---|
Description: | Curry, Compose, and other higher-order functions |
Authors: | Peter Danenberg <[email protected]> |
Maintainer: | Peter Danenberg <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.6 |
Built: | 2024-11-04 05:18:48 UTC |
Source: | https://github.com/cran/functional |
My Happy Hacking keyboard gave out during the writing of this procedure; moment of silence, please.
Compose(...)
Compose(...)
... |
the functions to be composed |
A composed function
car <- function(list) list[[1]] cdr <- function(list) list[2:length(list)] cadr <- Compose(cdr, car) stopifnot(cadr(c(1,2,3)) == 2)
car <- function(list) list[[1]] cdr <- function(list) list[2:length(list)] cadr <- Compose(cdr, car) stopifnot(cadr(c(1,2,3)) == 2)
Thanks, Byron Ellis. https://stat.ethz.ch/pipermail/r-devel/2007-November/047318.html
Curry(FUN, ...)
Curry(FUN, ...)
FUN |
the function to be curried |
... |
the determining parameters |
A new function partially determined
double <- Curry(`*`, e1=2) stopifnot(double(4) == 8)
double <- Curry(`*`, e1=2) stopifnot(double(4) == 8)
Lazy curry; thanks, Jamie! <https://github.com/klutometis/R-functional/issues/1>
CurryL(FUN, ...)
CurryL(FUN, ...)
FUN |
the function to be curried |
... |
the determining parameters |
# day is not defined; thanks, Jamie Folson. CurryL(function(...) match.call(), x=5, y=as.Date(day))(z=as.Date(day,"%Y"))
# day is not defined; thanks, Jamie Folson. CurryL(function(...) match.call(), x=5, y=as.Date(day))(z=as.Date(day,"%Y"))
Is concatenation benign?
Identity(...)
Identity(...)
... |
tautological arguments |
The tautologized arguments, concatenated
list.copy <- function(list) Reduce(Identity, list) list <- c(1, 2, 3) stopifnot(list.copy(list) == list)
list.copy <- function(list) Reduce(Identity, list) list <- c(1, 2, 3) stopifnot(list.copy(list) == list)
Thanks, Alexander Davis!
multi.argument.Compose(...)
multi.argument.Compose(...)
... |
the functions to be composed |
A composed function
f <- function(x, y) x+y g <- function(x) x*2 stopifnot(multi.argument.Compose(f, g)(1,1) == 4)
f <- function(x, y) x+y g <- function(x) x*2 stopifnot(multi.argument.Compose(f, g)(1,1) == 4)
Negate a function; borrowed from src/library/base/R/funprog.R for pre-2.7 Rs.
Negate(f)
Negate(f)
f |
the function to be negated |
The negated function
is.even <- function(a) a%%2 == 0 is.odd <- Negate(is.even) stopifnot(Reduce(`&&`, Map(is.odd, c(1, 3, 5))))
is.even <- function(a) a%%2 == 0 is.odd <- Negate(is.even) stopifnot(Reduce(`&&`, Map(is.odd, c(1, 3, 5))))
Thanks, Gabor; see <http://stackoverflow.com/a/23726989>: swaps the first two arguments in a function.
Swap(f)
Swap(f)
f |
The function whose arguments to swap |
A function with swapped arguments