Reference · Lists

any

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

What 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"))
output
truefalsetruetrue

Edge cases

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))
output
false falselooking at 1looking at 2truefalse true truetrue true

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(any(5, (x) => true))
output
line 1: `any` expects a list as argument 1, got int
this one fails
print(any([1, 2], true))
output
line 1: `any` expects a function as argument 2, got bool

Chaining

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))
output
a b namefalse

See also

all, find, contains, count, filter

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

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