Lists and maps
In the terminalela --spec lists-and-maps
A list's procs are in the list package and a map's in the map package, as text's are in
str. The top level keeps what works on any value or on numbers: len, has, type_of, int,
float, range, min and max (LANGUAGE.md, Core built-ins). Each proc gives
a new value and leaves the one it was given as it was.
| Built-in | Does |
|---|---|
list.append(list, items...) | The list with the items added at its end: xs = list.append($xs, x) adds them to xs in place, m[$k] = list.append($m[$k], x) to the list a map holds, and list.append($xs, ..$ys) joins two lists |
list.sort(list, by = null, desc = false, fold = false) | A new list in order: numbers by value, text by character code, false before true, null last; or a comparator proc's order, by name; desc = true the other way; fold = true text with case and accents folded (LANGUAGE.md, Sorting a list) |
list.sum(xs: list) | The total of a list of numbers: list.sum([1, 2, 3.5]) is 6.5, list.sum([]) is 0 (LANGUAGE.md, Totals, smallest and largest) |
list.head(xs: list, n: number) / list.tail(xs: list, n: number) | The first or last n items, all of them when the list is shorter (First and last items) |
map.keys(m) / map.values(m) | A map's keys or values as a list, in the map's order |
map.get(m, key, fallback) | A map's value at key, or fallback when the key is missing or null: totals[$k] = map.get($totals, $k, 0) + 1; $m["k"] on a missing key stays an error |
hosts = list.append($hosts, "db")names = list.sort(map.keys($config), fold = true)total = list.sum($sizes)for key in map.keys($config) echo $keyend for
A call to append(…), sort(…), sum(…), keys(…), values(…) or get(…), the names with no package,
stops before line 1 and names the proc, in the script, with -c and with ela --check:
deploy.ela:3:15: error: no proc named 'append'; ela's list procs are in list.: list.append(xs, item)
First and last items #
list.head(xs, n) and list.tail(xs, n) give up to n items from the start or the end of a
list, str.head(s, n) and str.tail(s, n) up to n characters of text, counted as $s[i]
counts them: all of it when it is shorter, so the call says "up to n" and needs no min. A slice
past the end stays an error, which finds a misread length at its line (Indexes and slices in
LANGUAGE.md). n is a whole number from 0; 0 gives an empty list or "".
last = list.tail(str.lines($out), 60) # the last 60 lines, or all of a shorter logtop = list.head($hosts, 3)part = str.tail($out, 3000) # the last 3000 charactersshort = str.head($commit, 8)