# Namespace goog.functions

code »
Show:Public Protected Private

## Global Functions

code »goog.functions.FALSE ( ) ⇒ `boolean`

Always returns false.

code »goog.functions.NULL ( ) ⇒ `null`

Always returns NULL.

code »goog.functions.TRUE ( ) ⇒ `boolean`

Always returns true.

code »goog.functions.and ( var_args ) ⇒ `function(?): boolean`

Creates a function that returns true if each of its components evaluates to true. The components are evaluated in order, and the evaluation will be short-circuited as soon as a function returns false. For example, (goog.functions.and(f, g))(x) is equivalent to f(x) && g(x).

Parameters
var_args: `...Function` A list of functions.
Returns
A function that ANDs its component functions.

code »`<T>` goog.functions.cacheReturnValue ( fn ) ⇒ `!function(): T`

Gives a wrapper function that caches the return value of a parameterless function when first called. When called for the first time, the given function is called and its return value is cached (thus this is only appropriate for idempotent functions). Subsequent calls will return the cached return value. This allows the evaluation of expensive functions to be delayed until first used. To cache the return values of functions with parameters, see goog.memoize.

Parameters
fn: `!function(): T` A function to lazily evaluate.
Returns
A wrapped version the function.

code »`<T>` goog.functions.compose ( fn, var_args ) ⇒ `function(?): T`

Creates the composition of the functions passed in. For example, (goog.functions.compose(f, g))(a) is equivalent to f(g(a)).

Parameters
fn: `function(?): T` The final function. var_args: `...Function` A list of functions.
Returns
The composition of all inputs.

code »`<T>` goog.functions.constant ( retValue ) ⇒ `function(): T`

Creates a function that always returns the same value.

Parameters
retValue: `T` The value to return.
Returns
The new function.

code »goog.functions.create ( constructor, var_args ) ⇒ `!Object`

Generic factory function to construct an object given the constructor and the arguments. Intended to be bound to create object factories. Callers should cast the result to the appropriate type for proper type checking by the compiler.

Parameters
constructor: `!Function` The constructor for the Object. var_args: `...*` The arguments to be passed to the constructor.
Returns
A new instance of the class given in `constructor`.

code »goog.functions.error ( message ) ⇒ `!Function`

Creates a function that always throws an error with the given message.

Parameters
message: `string` The error message.
Returns
The error-throwing function.

code »goog.functions.fail ( err ) ⇒ `!Function`

Creates a function that throws the given object.

Parameters
err: `*` An object to be thrown.
Returns
The error-throwing function.

code »`<T>` goog.functions.identity ( opt_returnValue, var_args ) ⇒ `T`

A simple function that returns the first argument of whatever is passed into it.

Parameters
opt_returnValue: `T=` The single value that will be returned. var_args: `...*` Optional trailing arguments. These are ignored.
Returns
The first argument passed in, or undefined if nothing was passed.

code »goog.functions.lock ( f, opt_numArgs ) ⇒ `!Function`

Given a function, create a function that keeps opt_numArgs arguments and silently discards all additional arguments.

Parameters
f: `Function` The original function. opt_numArgs: `number=` The number of arguments to keep. Defaults to 0.
Returns
A version of f that only keeps the first opt_numArgs arguments.

code »goog.functions.not ( f ) ⇒ `function(?): boolean`

Creates a function that returns the Boolean opposite of a provided function. For example, (goog.functions.not(f))(x) is equivalent to !f(x).

Parameters
f: `!Function` The original function.
Returns
A function that delegates to f and returns opposite.

code »goog.functions.nth ( n ) ⇒ `!Function`

Creates a function that returns its nth argument.

Parameters
n: `number` The position of the return argument.
Returns
A new function.

code »goog.functions.or ( var_args ) ⇒ `function(?): boolean`

Creates a function that returns true if any of its components evaluates to true. The components are evaluated in order, and the evaluation will be short-circuited as soon as a function returns true. For example, (goog.functions.or(f, g))(x) is equivalent to f(x) || g(x).

Parameters
var_args: `...Function` A list of functions.
Returns
A function that ORs its component functions.

code »goog.functions.sequence ( var_args ) ⇒ `!Function`

Creates a function that calls the functions passed in in sequence, and returns the value of the last function. For example, (goog.functions.sequence(f, g))(x) is equivalent to f(x),g(x).

Parameters
var_args: `...Function` A list of functions.
Returns
A function that calls all inputs in sequence.

code »`<T>` goog.functions.withReturnValue ( f, retValue ) ⇒ `function(?): T`

Given a function, create a new function that swallows its return value and replaces it with a new one.

Parameters
f: `Function` A function. retValue: `T` A new return value.
Returns
A new function.

## Compiler Constants

code »goog.functions.CACHE_RETURN_VALUE : `boolean`