[OpenSCAD] RFC: Array/String slicing and half open ranges.
rudolf at digitaldocument.de
Tue Nov 5 16:37:35 EST 2019
Very good idea to introduce a syntax for array slicing!
> echo(skip_last_exclusive = a[:<-1]);
> // ECHO: skip_last_exclusive = [0, 1, 2, 3, 4, 5, 6, 7, 8]
But to be honest: The sequence [:<- looks very cryptic and C-like. Also,
introducing and distinguishing between two interval types is too much
complication in my opinion. MATLAB style is much clearer, and you don't need
to distinguish interval types. It uses the reserved word 'end'.
echo(skip_last_exclusive = a[:end-1]); // Matlab style
// ECHO: skip_last_exclusive = [0, 1, 2, 3, 4, 5, 6, 7, 8]
Well, as 'end' is not a reserved word in OpenSCAD, what about '$' or better
'$end'? The latter might not break too much code, as using dynamic scope
variables is not encouraged, and cases, where '$end' is declared and used as
'variable', can be easily detected.
The usage is straight forward:
echo(skip_last = a[:$end-1]);
// ECHO: skip_last = [0, 1, 2, 3, 4, 5, 6, 7, 8]
echo(second_last = a[$end-1:$end-1]);
// ECHO: second_last = 
echo(last_two = a[$end-1:$end]); // alternative: last_two = a[$end-1:]
// ECHO: last_two = [8, 9]
In any case this syntax is intuitive, and you don't have to introduce and
distinguish a second (counter intuitive) interval type.
Sent from: http://forum.openscad.org/
More information about the Discuss