Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

compose

Compose multiple functions


Description

Compose multiple functions

Usage

compose(..., .dir = c("backward", "forward"))

Arguments

...

Functions to apply in order (from right to left by default). Formulas are converted to functions in the usual way.

These dots support tidy dots features. In particular, if your functions are stored in a list, you can splice that in with !!!.

.dir

If "backward" (the default), the functions are called in the reverse order, from right to left, as is conventional in mathematics. If "forward", they are called from left to right.

Value

A function

Examples

not_null <- compose(`!`, is.null)
not_null(4)
not_null(NULL)

add1 <- function(x) x + 1
compose(add1, add1)(8)

# You can use the formula shortcut for functions:
fn <- compose(~ paste(.x, "foo"), ~ paste(.x, "bar"))
fn("input")

# Lists of functions can be spliced with !!!
fns <- list(
  function(x) paste(x, "foo"),
  ~ paste(.x, "bar")
)
fn <- compose(!!!fns)
fn("input")

purrr

Functional Programming Tools

v0.3.4
GPL-3 | file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.