function_: Explicitly create closures.

View source: R/conversions.R

function_R Documentation

Explicitly create closures.

Description

function_ is a normally-evaluating version of function, which creates closures. A closure object has three components: the argument list, the body expression, and the enclosing environment.

arglist() is a helper that produces a named list of missing_values given a character vector of names.

Usage

function_(args, body, env = arg_env(args, environment()))

arglist(names, fill = missing_value())

Arguments

args

The argument list of the new function. NULL is accepted to make a function with no arguments. Arguments are specified as a named list; the list names become the argument names, and the list values become the default expressions. A value of missing_value() indicates no default. alist and arglist are useful for making argument lists.

body

An expression for the body of the function.

env

The enclosing environment of the new function.

names

A character vector.

fill

The expression (default missing)

Value

A closure.

See Also

environment formals body

Examples

f1 <- function(x, y = x) { x + y }
f2 <- function_(alist(x = , y = x),
                quote( { x + y } ),
                environment())
identical(f1, f2) # TRUE

# `fn` makes a compact way to write functions;
# `fn(x+y)` is equivalent to `function(x, y) x+y`
fn <- function(exp) {
  exp_ <- arg(exp)
  nn <- arglist(all.names(expr(exp_), functions=FALSE))
  function_(nn, expr(exp_), env(exp_))
}

fn(x^2)
fn(x+y)

crowding/nseval documentation built on Jan. 28, 2024, 2:10 a.m.