Reference · Lists

all

signature
all(list | null, fn) -> bool

What 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))
output
truefalsefalsetrue

Edge cases

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))
output
true truefalsechecking 1checking 2falsetrue false false

Errors

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.

this one fails
print(all("abc", (c) => c != " "))
output
line 1: `all` expects a list as argument 1, got str
this one fails
print(all([1, 2], "x"))
output
line 1: `all` expects a function as argument 2, got str

Chaining

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)))
output
all adultsfalse

See also

any, filter, count, find

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

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