eachPost (:O)
语法
eachPost(func, X, [post], [consistent=false])
或
X <operator>:O [post] (表示 consistent 为 false)
或
X <operator>:OC [post] (表示 consistent 为 true)
New in version 2.00.9.
或
func:O(X, [post], [consistent=false])
参数
func 是一个二元函数。
X 可以是向量、矩阵或表。当X是向量时,post必须是标量;当X是矩阵时,post必须是标量或向量;当X是表时,pre是标量或表;当post未指定时,结果的最后一个元素为NULL。
consistent 布尔值,默认 false,表示每个子任务输出的数据类型由实际计算结果决定。否则,每个子任务输出的数据类型将与第一个子任务输出的数据类型保持一致。 注意,若子任务输出的数据形式不一致,只能指定 consistent = false,否则会报错。
详情
将给定函数/运算符应用到所有相邻的数据对上。
eachPost 高阶函数等同于:F(X[0], X[1]), F(X[1], X[2]), …, F(X[n], post).
例子
$ x=1..10;
$ eachPost(sub, x);
[-1,-1,-1,-1,-1,-1,-1,-1,-1,]
// 等同于 [1-2, 2-3, ..., 9-10, NULL]
$ +:O x;
[3,5,7,9,11,13,15,17,19,]
// 等同于 [1+2, 2+3, ..., 9+10, NULL]
$ x +:O 0;
[3,5,7,9,11,13,15,17,19,10]
// 等同于 [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)
(8,12,[10])