Learn R Programming

dostats (version 1.3.3)

compose: Nest functions

Description

Nest functions

Usage

compose(..., .list)

x %.% y

Arguments

...

functions to be nested together

.list

alternatively an explicit list of functions. If specified ... will be ignored.

x

a function

y

a function

Value

new function consisting of the functions nested

Functions

  • %.%: infix compose operator

Details

compose creates a functional composition of the listed functions. Functional composition of functions f and g is defined as f(g(.)). Order matters the right most function listed will be the innermost function in the composition, same with the operator version. To remember the order lists will be the order read out, i.e. compose(f,g) = f(g(x))

When using the operator version it is good to remember that parentheses are recommended see the examples

Examples

Run this code
# NOT RUN {
compose(any, is.na)(c(NA,1:3))
(sum%.%is.na)(c(1,NA))  #correct
# }
# NOT RUN {
sum%.%is.an(NA)  #incorrect
# }

Run the code above in your browser using DataLab