odometer

From The K Language Wiki
Odometer
!I
Supported in k3(transposed), k4-k9
A real-life odometer.

The odometer for an int list x generates the Cartesian product of !x[0], !x[1], .., !x[-1+#x] as the columns of a #x by */x matrix.

 !2 4 3
(0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
 0 0 0 1 1 1 2 2 2 3 3 3 0 0 0 1 1 1 2 2 2 3 3 3
 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2)

Odometer generates all paths to elements in a matrix, with each column a path and each row a dimension. It can be used for indexing:

 : m:(`a`b`c;`d`e`f;`g`h`i)
(`a`b`c
 `d`e`f
 `g`h`i)
 : p: !2 2  / top-left corner
(0 0 1 1
 0 1 0 1)
 (m') . p
`a`b`d`e

Some K implementations(K2/K3) do not have this primitive. It can be defined as follows:

K3[1]

provides a transposed version.

{x _vs!*/x}

K6[2]

{+x\'!*/x}

References[edit]