Reference · Lists
any
any(list | null, fn) -> boolWhat it does
true when the function gives a true value for at least one item, else false. The
function takes one argument, the item, and is called in order; the search stops at the
first yes. 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 any(xs, f)? stops there.
Examples
print(any([1, 2, 3], (x) => x > 2))print(any([1, 2, 3], (x) => x > 5))let people = [{ name: "Ann", admin: false }, { name: "Bo", admin: true }]print(any(people, (p) => p.admin))print(any(["a", "b"], (s) => s == "b"))truefalsetruetrueEdge cases
- An empty list or
nullgivesfalse: there is no item to say yes. - The function is not called past the first match.
- The result is read for truth as
ifreads it: onlyfalseandnullare no, so0and""are yes. A function that returns the item givestruewhen some item is not null or false. - To ask whether a value is in a list, contains needs no function.
print(any([], (x) => true), any(null, (x) => true))print(any([1, 2, 3], (x) => { print("looking at", x) x > 1 }))print(any([null, false], (x) => x), any([null, 0], (x) => x), any([null, ""], (x) => x))print(any([1, 2], (x) => x == 2), contains([1, 2], 2))false falselooking at 1looking at 2truefalse true truetrue trueErrors
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(any(5, (x) => true))line 1: `any` expects a list as argument 1, got intprint(any([1, 2], true))line 1: `any` expects a function as argument 2, got boolChaining
In a pipeline the list comes first: xs | any(f). The result is a boolean, so it
reads well in a condition.
let names = ["ann", "bob"]print(names | any((n) => starts_with(n, "b")) ? "a b name" : "no b name")print(names | map(len) | any((n) => n > 5))a b namefalseSee also
all, find, contains, count, filter
Thread 0 posts
On any(): 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