Pair

Summary

In contrast to a scalar, a pair holds 2 values at a time. The two values need to be of the same data type. Pair is a special vector with two values.

A data pair can be used to:

(1)Represent data range.

In the following scenarios, the data range does not include the upper bound:

  • The pair represents offsets within an array/matrix/table.

$ x=5 3 6 2;
$ x[1:3];
[3,6]

$ for(s in 1:3){print s};
1
2

$ [5,6,8] between 1:6;
[1,1,0]
  • Used in a for-loop.

$ for(s in 1:3){print s};
1
2
$ bucket(4 0 1 3 2, 0:4, 2);
[,0,0,1,1]

In all other scenarios, the range is includes the upper bound.

$ [5,6,8] between 1:6;
[1,1,0]

$ t1 = table(`A`A`B as sym, 09:56:06 09:56:07 09:56:06 as time, 10.6 10.7 20.6 as price)
$ t2 = table(take(`A,10) join take(`B,10) as sym, take(09:56:00+1..10,20) as time, (10+(1..10)\10-0.05) join (20+(1..10)\10-0.05) as bid, (10+(1..10)\10+0.05) join (20+(1..10)\10+0.05) as offer, take(100 300 800 200 600, 20) as volume)
$ wj(t1, t2, -5:0, <avg(bid)>, `sym`time);

\(2) Illustrate the dimension of a matrix. e.g. “2:5” indicates a 2 by 5 matrix. If X is a one dimension array with 10 elements, statement “X\(2:5" converts X to a 2 by 5 matrix, where "\)” indicates

(3) Specify the capacity and initial number of rows of a table.

$ t=table(100:0, `date`sym`high`low`close, [DATE,SYMBOL,DOUBLE,DOUBLE,DOUBLE]);

Create Pairs

To create a pair, we use symbol colon “:” or function pair.

$ 1:3;

$ 3.4:7.8;

$ 1b:0b;
1:0
$ true:false;
1:0

$ 2013.06.13:2013.11.10;

$ 5 pair 6;
5:6

$ `Hello:`World;
"Hello" : "World"

Access Pairs

Use X[Y] to access pairs, where Y can be an integer, an integer vector or a pair.

$ x = 3:6;

$ x[1];
6

$ x[0 1];
[3,6]

Modify Pairs

$ x=3:6;

$ x[0]=4;
$ x;
4:6

$ x=3:6+1;
$ x;
4 : 7
// note the result is not 3:7