Qu

any

Builtin index · Collections & strings

result = all(xs, [predicate])

Input: xs, predicate · Output: result

What it is

With ONE argument, any(mask) / all(mask) take a boolean mask or numeric Vec/Mat and return whether any / every element is true (non-zero) — any(x > 3); an empty mask is false for any, true for all. With a predicate: xs is a Vec/List; predicate is a one-argument function returning a bool — the function itself (is_even, sqrt, (x) := x > 2), or a Str naming one you defined.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = all(xs, [predicate])
xs Vec or ListWith a predicate: xs is a Vec/List.
predicate bool or Str, optionalpredicate is a one-argument function returning a bool — the function itself (is_even, sqrt, (x) := x > 2), or a Str naming one you defined.
result bool or string
Returns a bool: whether every (all) or at least one (any) element satisfies it. As a quoted name a builtin is refused, because a string is indistinguishable from a typo; passed as a value, without quotes, it is allowed.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

function is_even(x)
    return x mod 2 == 0
end function

function add2(a, b)
    return a + b
end function

function double(x)
    return x * 2
end function

xs = [1, 2, 3, 4, 5]
print(all(xs, is_even))           # false

print(any(xs, is_even))           # true

print(first(xs))                 # 1

print(last(xs))                  # 5

print(take(xs, 3))                # (1, 2, 3)

print(distinct([1, 1, 2, 2, 3])) # (1, 2, 3) -- first-seen order

print(flatten([[1, 2], [3, 4]]))  # (1, 2, 3, 4)

print(fold(xs, add2, 0))          # 15 -- with an initial value

print(reduce(xs, add2))           # 15 -- without one

print(map(xs, double))            # (2, 4, 6, 8, 10) -- collection first

print(map(xs, (x) := x * 10))     # (10, 20, 30, 40, 50) -- or write it inline

print(map(xs, "double"))          # (2, 4, 6, 8, 10) -- the name as a string still works

print(xs.map(double))             # (2, 4, 6, 8, 10) -- and so the method form works

print(zip([1, 2, 3], ["a", "b", "c"]))  # ((1, "a"), (2, "b"), (3, "c"))

output
false
true
1
5
(1, 2, 3)
(1, 2, 3)
(1, 2, 3, 4)
15
15
(2, 4, 6, 8, 10)
(10, 20, 30, 40, 50)
(2, 4, 6, 8, 10)
(2, 4, 6, 8, 10)
((1, "a"), (2, "b"), (3, "c"))

from collections-strings

See also

all — documented in the same entry.