Higher Level Functions in R - Is There an Official Compose Operator or Curry Function

higher level functions in R - is there an official compose operator or curry function?

The standard place for functional programming in R is now the functional library.

From the library:

functional: Curry, Compose, and other higher-order functions

Example:

   library(functional)
newfunc <- Curry(oldfunc,x=5)

CRAN:
https://cran.r-project.org/web/packages/functional/index.html

PS: This library substitutes the ROxigen library.

Currying functions in R

It is possible to curry in R, and there is a definition in the ROxygen package. See the discussion here

How to pass some (but not all) further arguments with ...

One way is to just make value a named parameter but ignore the input - that way it will never be a part of the dots to begin with.

f <- function(pattern, x, value = "hahaThisGetsIgnored", ...){
grep(pattern, x, value = TRUE, ...)
}

You can also use the idea in the answer @MatthewLundberg gave but by doing the argument manipulation without Curry so you don't need the functional package

f <- function(pattern, x, ...){
dots <- list(...)
# Remove value from the dots list if it is there
dots$value <- NULL
args <- c(list(pattern = pattern, x = x, value = TRUE), dots)
do.call(grep, args)
}

How to concatenate/compose functions in R?

For this you can use the Compose function in the functional package, which is available on CRAN.:

library(functional)
sapply(x, Compose(unique,sum))

What is the practical use of the identity function in R?

Don't know about R, but in a functional language one often passes functions as arguments to other functions. In such cases, the constant function (which returns the same value for any argument) and the identity function play a similar role as 0 and 1 in multiplication, so to speak.

How to pass arguments to a function inside *apply family functions

You can pass arguments in all the functions of the apply family through the ellipsis (...) argument, cf. the help page on sapply. Now, apply.daily is just an extension to xts objects, see ?apply.daily.

 apply.daily(tt2, mean, na.rm=TRUE)
#apply.daily( x, FUN, ...)


Related Topics



Leave a reply



Submit