-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
List and Array operations
This is an attempt to collect ideas and discussion on list/array operations. NB! Below, we'll try to use "lists" as a concept covering what's sometimes called "arrays" or "vectors".
For current list comprehension functionality, see List Comprehensions
- concat()
- https://github.com/openscad/openscad/issues/201
- Vector append in RapCAD: http://www.gilesbathgate.com/2011/09/rapcad-dynamic-arrays/
- Using comma as concatenation operator:
newlist = lista, listb;
- Use the concatenation operator from the D programming language:
lista ~= listb; listc = lista ~ listb;
Ideas:
- Functional list paradigm: cons/car/cdr
- List slicing (extract subset of a list)
- Python notation:
list[a:b] (a through b-1), list[a:] (a to end), list[:b] (start to b-1), list[:] (entire list)
- Python notation:
- Picking indices
-
list2 = list[2,4]; // returns [3,1] ```
-
Use stack-like push / unshift
-
Declare lists in the same way as ranges
list = [2:0.5:4]; // Returns [2,2.5,3,3.5,4]
-
Support list comprehensions (map):
function double(list) = _ * 2;
list = [0,1,2,3];
list2 = double(list); // Returns [0,2,4,6]
(can already do this echo([0,1,2,3]*2); // = [0,2,4,6]
- Python-inspired syntax:
[for i = [a:b] f(i)];
- Advanced recursion example:
function a(b) = length(b) > 1 ? ( sin(b[0]) * 5, a(b[1:]) : sin(b[0]) * 5;
-
Random access:
list = [0,1,2,3]; list[1] = 4; // Returns [0,4,2,3];
-
Iteration over a vector
foreach (i=vector) {
...
}
-
Various math functions would help, my max/min/sum from http://www.thingiverse.com/thing:70745 for example, it would have been much easier if the built-in functions supported vectors. Where the vector is of the right type of course.
-
Much of the above can be done with recursive functions IF there was a vector concatenate operator or function, e.g.
v3=concat(v1,v2)