Reference · Lists
all
all(list | null, fn) -> boolWhat it does
true when the function gives a true value for every item, else false. The function
takes one argument, the item, and is called in order; the check stops at the first no.
null counts as an empty list.
An error the function gives comes back as the result: it has said neither yes nor no, and a
failed check must not look like nothing to see. So all(xs, f)? stops there.
Examples
print(all([2, 4, 6], (x) => x % 2 == 0))print(all([2, 3, 6], (x) => x % 2 == 0))let people = [{ name: "Ann", age: 30 }, { name: "Bo", age: 15 }]print(all(people, (p) => p.age >= 18))print(all(["a", "b"], (s) => len(s) == 1))truefalsefalsetrueEdge cases
- An empty list or
nullgivestrue: there is no item to say no. Checklenfirst when an empty list must not pass. - The function is not called past the first item that fails.
- The result is read for truth as
ifreads it: onlyfalseandnullare no, soall(xs, (x) => x)istruewhen no item is null or false;0and""pass.
print(all([], (x) => false), all(null, (x) => false))let xs = []print(len(xs) > 0 && all(xs, (x) => x > 0))print(all([1, 2, 3], (x) => { print("checking", x) x < 2 }))print(all([1, 0, ""], (x) => x), all([1, null], (x) => x), all([1, false], (x) => x))true truefalsechecking 1checking 2falsetrue false falseErrors
A first argument that is not a list or null, or a second that is not a function, is
a runtime error, not an error value. An error inside the function stops the request.
print(all("abc", (c) => c != " "))line 1: `all` expects a list as argument 1, got strprint(all([1, 2], "x"))line 1: `all` expects a function as argument 2, got strChaining
In a pipeline the list comes first: xs | all(f). It reads well as a condition, and
after a map that computes what to check.
let ages = [30, 25, 41]print(ages | all((a) => a >= 18) ? "all adults" : "not all adults")print(["1", "2", "x"] | map(int) | all((v) => !is_err(v)))all adultsfalseSee also
Thread 0 posts
On all(): 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