loop / ploop
Syntax
loop(func, args…)
Arguments
func is a function.
args … are the required parameters of func.
Details
The loop
template is very similar to the each
template. Their difference is about the data form and data type of the function call results.
For the each template, the data types and forms of the return value are determined by each calculation result. It returns a vector or matrix if all calculation results have the same data type and form, otherwise it returns a tuple.
The
loop
template always returns a tuple.
Examples
In the examples below, loop
returns a tuple containing two data forms: scalar and vector.
$ loop(call{, 3 4 5}, (sum, log));
(12,[1.098612,1.386294,1.609438])
// call{,x} is a partial application that takes another function as input.
$ loop(call{, 3 4 5}, (log, sum));
([1.098612,1.386294,1.609438],12)
In the example below, loop
calculates the maximum value of each column in a table.
$ t = table(1 2 3 as id, 4 5 6 as value, `IBM`MSFT`GOOG as name);
$ t;
id |
value |
name |
---|---|---|
1 |
4 |
IBM |
2 |
5 |
MSFT |
3 |
6 |
GOOG |
$ loop(max, t.values());
(3,6,"MSFT")