# eachPre (:P)

Syntax

eachPre(func, X, [pre])

or

[pre] <operator>:P X

Arguments

func is a binary function.

X is a vector/matrix/table.

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

Details

Apply func over all pairs of consecutive elements of X. It is equivalent to: F(X[0], pre), F(X[1], X[0]), …, F(X[n], X[n-1]).

The built-in functions ratios and deltas are also implemented by the `eachPre` template. They are defined as:

• function deltas(a){return a[0] -:P a}

• function ratios(a){return a[0] :P a}

Examples

```\$ x=1..10;

\$ eachPre(sub, x);
[,1,1,1,1,1,1,1,1,1]
// equivalent to [NULL, 2-1, ..., 10-9]

\$ -:P x;
[,1,1,1,1,1,1,1,1,1]
// same as above

\$ eachPre(+, x);
[,3,5,7,9,11,13,15,17,19]
// equivalent to [NULL, 2+1, ..., ]

\$ 0 +:P x;
[1,3,5,7,9,11,13,15,17,19]
// equivalent to [1+0, 2+1, ..., ]

\$ x=1..12\$3:4;
x;
```

col1

col2

col3

col4

1

4

7

10

2

5

8

11

3

6

9

12

```\$ - :P x;
```

col1

col2

col3

col4

3

3

3

3

3

3

3

3

3

```\$ eachPre(\, x, x[0]);
```

col1

col2

col3

col4

1

4

1.75

1.428571

1

2.5

1.6

1.375

1

2

1.5

1.333333