forked from OskarLinde/scad-utils
-
Notifications
You must be signed in to change notification settings - Fork 48
/
lists.scad
48 lines (36 loc) · 1.01 KB
/
lists.scad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// List helpers
/*!
Flattens a list one level:
flatten([[0,1],[2,3]]) => [0,1,2,3]
*/
function flatten(list) = [ for (i = list, v = i) v ];
/*!
Creates a list from a range:
range([0:2:6]) => [0,2,4,6]
*/
function range(r) = [ for(x=r) x ];
/*!
Reverses a list:
reverse([1,2,3]) => [3,2,1]
*/
function reverse(list) = [for (i = [len(list)-1:-1:0]) list[i]];
/*!
Extracts a subarray from index begin (inclusive) to end (exclusive)
FIXME: Change name to use list instead of array?
subarray([1,2,3,4], 1, 2) => [2,3]
*/
function subarray(list,begin=0,end=-1) = [
let(end = end < 0 ? len(list) : end)
for (i = [begin : 1 : end-1])
list[i]
];
/*!
Returns a copy of a list with the element at index i set to x
set([1,2,3,4], 2, 5) => [1,2,5,4]
*/
function set(list, i, x) = [for (i_=[0:len(list)-1]) i == i_ ? x : list[i_]];
/*!
Remove element from the list by index.
remove([4,3,2,1],1) => [4,2,1]
*/
function remove(list, i) = [for (i_=[0:1:len(list)-2]) list[i_ < i ? i_ : i_ + 1]];