Skip to content

compute indexspace

kgryte edited this page May 12, 2015 · 1 revision

Generates a linearly spaced index array from a subsequence string. len specifies the output index array length.

var arr = compute.indexspace( ':', 5 );
// returns [ 0, 1, 2, 3, 4 ]

arr = compute.indexspace( '2:', 5 );
// returns [ 2, 3, 4 ]

arr = compute.indexspace( '1:4:2', 5 );
// returns [ 1, 3 ]

arr = compute.indexspace( '-3:', 5 );
// returns [ 2, 3, 4 ];

arr = compute.indexspace( ':-2', 5 );
// returns [ 0, 1, 2 ]

arr = compute.indexspace( ':', 0 );
// returns []

The subsequence string syntax is similar to Python's slice notation.

var str = '<start>:<stop>:<increment>';

The method also recognizes the end keyword, which refers to the last index; i.e., len-1. If specified as the stop index, end is inclusive and equivalent to <start>::<increment>.

arr = compute.indexspace( 'end::-1', 5 );
// returns [ 4, 3, 2, 1, 0 ]

arr = compute.indexspace( ':end', 5 );
// returns [ 0, 1, 2, 3, 4 ]

Basic arithmetic (subtraction and division) may be performed on the end keyword. The result from division is rounded up to the next integer.

arr = compute.indexspace( 'end-2::-1', 5 );
// returns [ 2, 1, 0 ];

arr = compute.indexspace( ':end/2', 5 );
// returns [ 0, 1 ]

For further details about syntax and additional examples, see compute-indexspace.

Clone this wiki locally