Reference · Maps

set

signature
set(map | list | null, any, any=) -> map | list

What 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"))
output
{ name: "Ann", age: 41 } { name: "Ann" }{ name: "Bob", age: 41 }[1, "b", 3] [1, 2, "z"]

Edge cases

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))
output
{ 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.

this one fails
print(set([1, 2], 5, 0))
output
line 1: `set`: index 5 is outside a list of 2

Chaining

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))
output
{ a: 1, b: 2, c: 3 }[0, 9, 0]

See also

get, remove, merge, has, push

Thread
6Useful? Like it, or add to its thread below.

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