1 The [manual chapter on arrays](https://ucode-lang.org/manual/) covers the 2 value type, indexing semantics and every builtin in detail. The examples below 3 collect implementations of frequently needed operations that are not part of 4 the core library, plus one pitfall worth knowing. 5 6 ## Mapping builtin functions 7 8 `map()` (and `filter()`, `sort()`, ...) invoke their callback with the 9 element, its index and the array itself. Passing a builtin that takes 10 optional arguments directly therefore feeds it unintended values: 11 12 ```ucode 13 let strings = [ "10", "32", "13" ]; 14 15 map(strings, int); // [ 10, "NaN", 1 ] - the index is read as the base! 16 17 map(strings, (x) => int(x)); // [ 10, 32, 13 ] 18 ``` 19 20 Wrap such builtins in an arrow function whenever they take more than one 21 argument with defaults, e.g. `int()`, `substr()`, `match()`. 22 23 ## Set operations 24 25 The `in` operator makes membership testing concise: 26 27 ```ucode 28 function intersect(...arrays) { 29 if (!length(arrays)) 30 return []; 31 32 let result = arrays[0]; 33 34 for (let i = 1; i < length(arrays); i++) 35 result = filter(result, (item) => item in arrays[i]); 36 37 return uniq(result); 38 } 39 40 intersect([ 1, 2, 3, 4 ], [ 2, 3, 5 ], [ 2, 3, 6 ]); // [ 2, 3 ] 41 ``` 42 43 ```ucode 44 function difference(array, ...others) { 45 return filter(array, (item) => { 46 for (let other in others) 47 if (item in other) 48 return false; 49 50 return true; 51 }); 52 } 53 54 difference([ 1, 2, 3, 4, 5 ], [ 2, 3 ], [ 4 ]); // [ 1, 5 ] 55 ``` 56 57 ## Merging, chunking, summing, flattening 58 59 The variadic `push()` combined with the spread operator merges arrays: 60 61 ```ucode 62 function merge(...arrays) { 63 let result = []; 64 65 for (let arr in arrays) 66 push(result, ...arr); 67 68 return result; 69 } 70 71 merge([ 1, 2 ], [ 3, 4 ], [ 5, 6 ]); // [ 1, 2, 3, 4, 5, 6 ] 72 ``` 73 74 ```ucode 75 function chunk(array, size) { 76 let result = []; 77 78 for (let i = 0; i < length(array); i += size) 79 push(result, slice(array, i, i + size)); 80 81 return result; 82 } 83 84 chunk([ 1, 2, 3, 4, 5, 6, 7, 8 ], 3); // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ] 85 ``` 86 87 ```ucode 88 function sum(array) { 89 let result = 0; 90 91 for (let item in array) 92 if (type(item) == "int" || type(item) == "double") 93 result += item; 94 95 return result; 96 } 97 98 sum([ 1, 2, 3, 4, 5 ]); // 15 99 ``` 100 101 ```ucode 102 function flatten(array, depth) { 103 depth ??= 1; 104 105 let result = []; 106 107 for (let item in array) { 108 if (type(item) == "array" && depth > 0) { 109 for (let sub in flatten(item, depth - 1)) 110 push(result, sub); 111 } else 112 push(result, item); 113 } 114 115 return result; 116 } 117 118 flatten([ 1, [ 2, [ 3, 4 ], 5 ], 6 ]); // [ 1, 2, [ 3, 4 ], 5, 6 ] 119 flatten([ 1, [ 2, [ 3, 4 ], 5 ], 6 ], 2); // [ 1, 2, 3, 4, 5, 6 ] 120 ``` 121 122 ## Deep copying 123 124 Arrays and objects are reference types, so copies must be made explicitly; 125 note the forward declaration, as ucode does not hoist function declarations: 126 127 ```ucode 128 function deepCopyObject; 129 130 function deepCopy(arr) { 131 if (type(arr) != "array") 132 return arr; 133 134 let result = []; 135 136 for (let item in arr) { 137 if (type(item) == "array") 138 push(result, deepCopy(item)); 139 else if (type(item) == "object") 140 push(result, deepCopyObject(item)); 141 else 142 push(result, item); 143 } 144 145 return result; 146 } 147 148 function deepCopyObject(obj) { 149 if (type(obj) != "object") 150 return obj; 151 152 let result = {}; 153 154 for (let key in keys(obj)) { 155 if (type(obj[key]) == "array") 156 result[key] = deepCopy(obj[key]); 157 else if (type(obj[key]) == "object") 158 result[key] = deepCopyObject(obj[key]); 159 else 160 result[key] = obj[key]; 161 } 162 163 return result; 164 } 165 ``` 166 167 The object half of this pair is re-used by the 168 [deep merge recipe](tutorial-03-objects.html).
This page was automatically generated by LXR 0.3.1. • OpenWrt