Is there something like a function composition in R?
I think in haskell it's somthing like "(.)" and in agda it's the ring operator.
Also, I find litte information on high level functional programming in R. I found the Functions "Reduce", "Map", "Filter"..., are there more? Any pointers?
To understand functions in R you need to internalise two important ideas: Functions can be broken down into three components: arguments, body, and environment.
In Maths, the composition of a function is an operation where two functions say f and g generate a new function say h in such a way that h(x) = g(f(x)). It means here function g is applied to the function of x. So, basically, a function is applied to the result of another function.
The functional
package has a Compose
functional which generalizes to any number of functions:
set.seed(123)
x <- matrix(runif(100), 10, 10)
mean(rowSums(scale(x)))
# [1] 5.486063e-18
library(functional)
Compose(scale, rowSums, mean)(x)
# [1] 5.486063e-18
(Note that the functions are applied from left to right.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With