KeyV2/libraries/scad-utils/lists.scad

49 lines
1.0 KiB
OpenSCAD
Raw Normal View History

2017-11-12 09:32:27 +11:00
// 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]];