Reference · Maps
set
set(map | list | null, any, any=) -> map | listWhat it does
A new map with one entry set, or a new list with one item replaced. On a map,
set(m, k, v) puts v under the key k (turned into text, as all map keys are),
adding the entry at the end or replacing the value in place. On a list, set(xs, i, v)
replaces the item at position i, counted from the end when negative. Values never
change: the original map or list stays as it was.
Examples
let user = { name: "Ann" }let older = set(user, "age", 41)print(older, user)print(set(older, "name", "Bob"))print(set([1, 2, 3], 1, "b"), set([1, 2, 3], -1, "z")){ name: "Ann", age: 41 } { name: "Ann" }{ name: "Bob", age: 41 }[1, "b", 3] [1, 2, "z"]Edge cases
- A key already there keeps its position; a new key goes last.
- Any key becomes text:
5sets"5",truesets"true", a decimal drops its trailing zeros (1.50msets"1.5"), a list sets its text. - Without a value, the entry is set to
nullandhasstill finds it. - An error value as the key is given back, and nothing is set: its text was taken for a key. The same in a loop that changes the map in place.
nullas the map is an empty map.- Built up in a loop or in
reduce, a map no one else holds is changed in place, so a map of thousands of entries costs milliseconds.
let m = { b: 2, a: 1 }print(set(m, "a", 10), set(m, 5, "five"), set(m, 1.50m, "x"))print(set(m, "c"), has(set(m, "c"), "c"))print(set(null, "a", 1))print(range(5) | reduce({}, (acc, i) => set(acc, i, i * i)))print(set(m, int("q"), 1)){ b: 2, a: 10 } { b: 2, a: 1, "5": "five" } { b: 2, a: 1, "1.5": "x" }{ b: 2, a: 1, c: null } true{ a: 1 }{ "0": 0, "1": 1, "2": 4, "3": 9, "4": 16 }err("not an int: \"q\"")Errors
A list index past either end is a runtime error, not an error value: push adds an
item. A list index that is not an int, and a first argument that is not a map, a
list or null, are runtime errors too.
print(set([1, 2], 5, 0))line 1: `set`: index 5 is outside a list of 2Chaining
In a pipeline the map or list comes first:
print({ a: 1 } | set("b", 2) | set("c", 3))print([0, 0, 0] | set(1, 9)){ a: 1, b: 2, c: 3 }[0, 9, 0]See also
Thread 0 posts
On set(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo