curry: Partial function application

Description Usage Arguments Details Value Note See Also Examples

View source: R/functional.R

Description

curry and related functions modify other functions by pre-setting their arguments.

Usage

1
2
3
4
5

Arguments

f

The function to curry or uncurry.

...

Arguments to be used in currying.

Details

Strictly speaking, these functions do partial application rather than currying, but they can be used to implement proper currying if desired, and the abuse of terminology is common. For an introduction to the whole concept of function currying and why it's useful, in more detail than we can give here, see the ever-helpful Wikipedia. curry uses standard evaluation (i.e., does not implicitly quote its arguments), while lazy.curry uses substitute to avoid evaluating its arguments before using them in currying.

uncurry takes a curried function - one resulting from a call to curry or lazy.curry - and undoes the currying, returning the original function without pre-set arguments.

If currying is nested, one call to uncurry can be made for each call to curry or lazy.curry - attempting to uncurry a function more times than it's been curried will raise an error.

Value

For curry and lazy.curry, the curried function; for uncurry, the uncurried function. Note that uncurry will raise an error if its argument did not result from a call to curry or lazy.curry.

Note

Currying is named after the mathematician Haskell Curry.

See Also

compose, which is frequently useful in conjunction with the currying and uncurrying functions.

Examples

1
2
3
4
5
6
7
8
9
#Equivalent
f <- function(x) x + 6
g <- curry(`+`, 6)

4 + 2 == 6
curry(`+`, 2)(4) == 6

paste0("foo", "bar") == "foobar"
uncurry(curry(paste0, "baz"))("foo", "bar") == "foobar"

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