How to concatenate/compose functions in R?

functional-programming, r

Solution

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

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

Problem

Often I want to do something like the following: ``` sapply(sapply(x, unique),sum) ``` Which I can also write as: ``` sapply(x, function(y) sum(unique(y))) ``` But I don't like to write out the lambda each time. So is there some kind of function that lets me write? ``` sapply(x, concat(sum,unique)) ``` And `concat` just concatenates the two functions, thus creates a new one, which executes one after the other.

Original source