eachLeft (:L)

Syntax

eachLeft(func, X, Y, [consistent=false])

or

X <operator>:L Y (when consistent = false)

or

X <operator>:LC Y (when consistent = true)

or

func:L(X, Y, [consistent=false])

Arguments

func is a binary function.

X/ Y is a vector/matrix/table/dictionary.

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

Calculate func(X(i),Y) for each element of X.

  • X(i) is each element when X is a vector.

  • X(i) is each column when X is a matrix.

  • X(i) is each row when X is a table.

  • X(i) is each value when X is a dictionary.

If func supports vector operation and the input is a vector, we should use the vector function/operator directly instead of the eachLeft template for better performance.

Examples

eachLeft with 2 vectors:

$ x = 4 3 2 1
$ y = 3 0 6;
$ x +:L y;

4

3

2

1

7

6

5

4

4

3

2

1

10

9

8

7

$ eachLeft(pow, x, y);

4

3

2

1

64

27

8

1

1

1

1

1

4096

729

64

1

eachLeft with a vector and a matrix:

$ x=1..6$2:3;
$ x;

col1

col2

col3

1

3

5

2

4

6

$ x ** :L 1 1;
[3,7,11]

eachLeft with 2 matrices:

$ y=6..1$2:3;
$ y;

col1

col2

col3

6

4

2

5

3

1

$ z = x **:L y;
$ z;

(#0 #1 #2
-- -- --
16 10 4
,#0 #1 #2
-- -- --
38 24 10
,#0 #1 #2
-- -- --
60 38 16
)
$ typestr z;
ANY VECTOR

eachLeft with a dictionary and a vector:

$ d=dict(`a`b`c, [[1, 2, 3],[4, 5, 6], [7, 8, 9]])
$ eachLeft(add,d,10 20 30)
a->[11,22,33]
b->[14,25,36]
c->[17,28,39]