lambda: Create Functions from Formulas/Objects

Description Usage Arguments Details Value Examples

Description

lambda allows the quick creation of anonymous functions from a variety of different object types, such as formulas or from other calls.

Usage

1
lambda(x, ...)

Arguments

x

object to be converted to a function

...

arguments passed to methods

Details

The behavior of lambda depends on the object that is passed to it. The method for handling functions returns the function unchanged. The method for handling symbols evaluates the symbol, then attempts to apply itself to the result. For calls, lambda creates a function that applies the new arguments, along with the original arguments in the call, to the original call function.

lambda attempts to parse a formula object - an object with syntax LHS ~ RHS - by using the value on the left-hand side as the function arguments and the value on the right-hand side as the function body. The argument on the left-hand side is split across "." so that multiple arguments can be easily created. For example, lambda(x.y ~ sqrt(x + y)) creates function(x, y) sqrt(x + y). If the formula is only one-sided, then the formula is parsed similar to the method in the purrr package; names that are prefixed by a "." are considered the function arguments. The previous function could also be created using lambda(~sqrt(.x + .y)).

lambda is used in many of the higher-order functions in the eList package. It can also be used in other functions so that users have a variety of options in which they satisfy functional arguments.

Value

function

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
double2 <- lambda(x.y ~ 2*(x + y))
double2(5, 6)

# alternatively, using 'dot' notation:
double2 <- lambda(~ 2*(.x + .y))

# using a call with partial arguments
subcall <- substitute(round(digits=2))
round2 <- lambda(subcall)
round2(0.04393282)

cmann3/eList documentation built on Jan. 25, 2021, 6:17 a.m.