[OpenSCAD] RFC: Array/String slicing and half open ranges.

Torsten Paul Torsten.Paul at gmx.de
Fri Nov 1 23:55:07 EDT 2019


I'm looking at making array (and string) handling more
flexible by adding slicing.

This would use the range syntax [begin:step:end] but
the interpretation of values is different from actual
ranges. Specifically negative values produce indexes
counted from the end of the array.

In addition I'm trying to add a way to express that
the range/slice is supposed to be a half-open interval.
So far the way Swift is doing that seems to fit best.
Translated to OpenSCAD syntax, this would looks like:
[begin:<end] or [begin:step:<end].

(Please don't suggest [begin:end) - that's not going
to happen even though it's tempting. ;-)


Examples using a = [0,1,2,3,4,5,6,7,8,9];

echo(skip_first = a[1:]);
// ECHO: skip_first = [1, 2, 3, 4, 5, 6, 7, 8, 9]

echo(skip_last_inclusive = a[:-2]);
// ECHO: skip_last_inclusive = [0, 1, 2, 3, 4, 5, 6, 7, 8]

echo(skip_last_exclusive = a[:<-1]);
// ECHO: skip_last_exclusive = [0, 1, 2, 3, 4, 5, 6, 7, 8]

For more examples, see the description on github:

https://github.com/openscad/openscad/pull/3115

ciao,
  Torsten.




More information about the Discuss mailing list