diff options
Diffstat (limited to 'js/scripting-lang/scratch_tests/flatten_scrap.txt')
-rw-r--r-- | js/scripting-lang/scratch_tests/flatten_scrap.txt | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/js/scripting-lang/scratch_tests/flatten_scrap.txt b/js/scripting-lang/scratch_tests/flatten_scrap.txt new file mode 100644 index 0000000..e5d5c96 --- /dev/null +++ b/js/scripting-lang/scratch_tests/flatten_scrap.txt @@ -0,0 +1,25 @@ +/* Problem: Flatten nested tables */ +nested : { + level1: { + a: {value: 1}, + b: {value: 2} + }, + level2: { + c: {value: 3} + } +}; + +/* Recursive flattening function */ +flatten : table -> + when (t.has table "value") is + true then table + _ then reduce @t.merge {} (map @flatten_entry table); + +flatten_entry : entry -> + when (t.has entry "value") is + true then entry + _ then flatten entry; + +/* Apply flattening */ +flat : flatten nested; +/* Result: {a: {value: 1}, b: {value: 2}, c: {value: 3}} */ |