eachPost

Syntax

eachPost(func, X, [post], [consistent=false])

or

X <operator>:O [post] (when consistent = false)

or

X <operator>:OC [post] (when consistent = true)

or

func:O(X, [post]) (when consistent = false)

or

func:OC(X, [post]) (when consistent = true)

Arguments

func is a binary function.

X is a vector/matrix/table. When X is a matrix, post must be a scalar or vector; when X is a table, post must be a scalar or table. When post is absent, the first element in the result would be NULL.

consistent is a Boolean value. The default value is false, indicating that the data type of the result is determined by each calculation result. Otherwise, the data type of the result is the same as the data type of the first calculation result. Note that if the data forms of result are inconsistent, consistent can only be specified as false. Otherwise, an error will be reported.

Details

Apply func over all pairs of consecutive elements of the object. It is equivalent to: F(X[0], X[1]), F(X[1], X[2]), ..., F(X[n], post).

Examples

x=1..10;
eachPost(sub, x);
// output
[-1,-1,-1,-1,-1,-1,-1,-1,-1,]
// equivalent to [1-2, 2-3, ..., 9-10, NULL]

+:O x;
// output
[3,5,7,9,11,13,15,17,19,]
// equivalent to [1+2, 2+3, ..., 9+10, NULL]

x +:O 0;
// output
[3,5,7,9,11,13,15,17,19,10]
// equivalent to [1+2, 2+3, ..., 9+10, 10+0]

x=1..12$3:4;
x;
col1 col2 col3 col4
1 4 7 10
2 5 8 11
3 6 9 12
-:O x;
col1 col2 col3 col4
-3 -3 -3
-3 -3 -3
-3 -3 -3
eachPost(\, x, x[0]);
col1 col2 col3 col4
0.25 0.571429 0.7 10
0.4 0.625 0.727273 5.5
0.5 0.666667 0.75 4
def f1(a,b){
    return (a[`x])+(a[`y])+(b[`x])+(b[`y])
}

t = table(1 2 3 as x,2 3 4 as y)
t1 = table(1 as x,2 as y)
eachPost(f1,t,t1)
// output
(8,12,[10])