apply() Function

Check out the new looping function, a!forEach(). It does everything apply() does but with easier syntax, better null handling, and support for interface components.

Calls a rule or function for each item in a list, and provides any contexts specified.

Syntax

apply( function, list, [context, … ] )

function: (Rule or Function Reference) Rule or expression function.

list: (Any Type Array) List of elements that function iterates through.

context: (Any Type Array) Variable number of parameters passed directly into each function evaluation.

Returns

Any Type Array

Notes

Use fn!functionName to reference an expression function and rule!ruleName to reference a rule.

Null lists return a null list without executing the function.

To use rules or functions that take more than one argument, use the merge() function. For example, given a rule g(x, y), apply(rule!g, merge({a, b, c}, {d, e, f})) returns {g(a, d), g(b, e), g(c, f)}.

The result of each operation is appended to each other in the same order as their corresponding item in the input list. If the result of each operation is an array, apply() returns a two-dimensional array which can then be used for further computation. When the two-dimensional array is saved into a process variable, a node input or a custom data type, the array is flattened to a one-dimensional array. Local variables created using the with() and load() functions, however, can store the two-dimensional array without flattening it.

See also: with() and load()

If you save the nested arrays into a process variable for multiple values, the nested function is flattened. Keep in mind that casting to a flattened array only happens when saving into a process variable, node input, custom data type, or custom data type field.

See also: Arrays in Expressions

To avoid having the nested function flatten, you can use the output of the apply() function as the input for a merge() function.

See below: merge()

apply() cannot be used with rules or functions that store local data, including rules using load() and certain interface components. In these cases, apply() will return an error. In these cases, use a!applyComponents().

See also: load() and a!applyComponents().

Examples

You can experiment with this function in the test box below.

Test Input

Given a function h(x, y), apply(fn!h, {a, b, c}, v) returns {h(a, v), h(b, v) ,h(c, v)}

apply(fn!sum,{-1,2,3},2) returns 1, 4, 5

FEEDBACK