mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-10 16:54:23 +02:00
add assoc
This commit is contained in:
14
src/experimental/_impl/_assoc_impl.scad
Normal file
14
src/experimental/_impl/_assoc_impl.scad
Normal file
@@ -0,0 +1,14 @@
|
||||
use <util/slice.scad>;
|
||||
|
||||
function _assoc_get(array, key) =
|
||||
let(idx = search([key], array)[0])
|
||||
array[idx][1];
|
||||
|
||||
function _assoc_put(array, key, value) =
|
||||
let(idx = search([key], array)[0])
|
||||
idx == [] ? concat(array, [[key, value]]) :
|
||||
concat(slice(array, 0, idx), [[key, value]], slice(array, idx + 1));
|
||||
|
||||
function _assoc_remove(array, key) =
|
||||
let(idx = search([key], array)[0])
|
||||
idx == [] ? array : concat(slice(array, 0, idx), slice(array, idx + 1));
|
9
src/experimental/assoc.scad
Normal file
9
src/experimental/assoc.scad
Normal file
@@ -0,0 +1,9 @@
|
||||
use <experimental/_impl/_assoc_impl.scad>;
|
||||
|
||||
// treat an associative array as a dictionary.
|
||||
|
||||
function assoc(array, cmd, key, value) =
|
||||
is_undef(array) ? [] :
|
||||
cmd == "put" ? _assoc_put(array, key, value) :
|
||||
cmd == "get" ? _assoc_get(array, key) :
|
||||
cmd == "remove" ? _assoc_remove(array, key) : assert("Uknown command");
|
Reference in New Issue
Block a user