compose: Compose functions

Description Usage Arguments Details Value See Also Examples

View source: R/functional.R

Description

Function composition is a common operation in functional programming. Given some number of input functions, these functions return new functions which represents the composition of the inputs. That is, for three functions f, g, h, compose(f, g, h) returns a function equivalent for all x to f(g(h(x))). composeM handles the case of functions of multiple arguments.

Usage

1
2
3
compose(..., where = parent.frame())

composeM(..., where = parent.frame())

Arguments

...

Objects to compose. See 'Details'.

where

An environment in which to resolve any character or symbol arguments.

Details

For compose, the functions passed must all take a single argument.

For composeM, the functions passed must take n >= 1 arguments, and return a list or vector. composeM's returned function proceeds right to left through the functions it composes, applying each to the sequence of arguments returned by the previous function (via do.call). The first function is called with the actual arguments passed.

If some of the ... arguments are not function objects, they are resolved to function objects in the following way: character arguments are looked up in the provided where environment; symbol arguments are coerced to character and then looked up in the where environment; other types of arguments, or symbol/character arguments which fail to resolve to a function object, raise an error.

Value

A function representing the composition of the arguments.

See Also

curry, lazy.curry and uncurry, which are frequently useful in conjunction with compose.

Examples

1
2
3
4
5
f <- function(x) x+1
g <- function(x) 3*x
h <- function(x) x^2

compose(f, g, h)(2) #=> 13 == 3(2)^2 + 1

wwbrannon/schemeR documentation built on May 4, 2019, 12:03 p.m.