Function application
Syntax
$fun($a0, $a1, ..., $an)
$fun.apply($a0, $a1, ..., $an)
Description
Evaluates the fun
function and passes the result of the evaluations of the a1, ..., an
expressions as arguments.
The syntax with the explicit apply(...)
may only be used for user-defined functions defined in the same script. Expressions like date.apply(2015, 8, 6)
are not possible.
When using a function, the function can be called on the first argument or the first argument is written with in the brackets:
3.14.round(1)
is equivalent to
round(3.14, 1)
Parameter
Name | Type | Description | Mandatory | Default |
---|---|---|---|---|
fun | Function | A function to be executed. | yes | |
a | number/string/boolean/object/collection | One or more parameters to pass to the function. | yes |
Examples
Function with one parameter
{
myFunc = x -> 5 * $x;
$myFunc.apply(8);
}
Output: 40
The function receives the number 8 as parameter and multiplies it by 5.
Function with two parameters
{
myFunc = x -> y -> $x * $y;
$myFunc.apply(8, 2);
}
Output: 16
Multiplies the two parameters 8 and 2 with each other.
Direct function application without function variable (apply)
(x -> y -> $x * $y).apply(8,3)
Output: 24
Functions do not have to be stored in a variable to be called. Apply can also be called directly.
Direct function application without function variable
(x -> y -> $x * $y)(8,3)
Output: 24