CH05-L01 · applied

The all() and exists() macros

Test a condition for every item and find at least one match.

General CELRuns hereDocumented
What will I learn?

You can distinguish a condition for every item from one requiring at least one match.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

Collection macros bind one temporary variable for each item. all(score, ...) returns true only if every score passes its predicate. exists(score, ...) returns true when at least one score passes. On an empty list, all() is true and exists() is false.

Read the prepared example

The CEL + JSON rule is:

scores.all(score, score >= 50.0) && scores.exists(score, score >= 90.0)

Its input is:

{
  "scores": [72, 94, 81]
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. Every score is at least 50.0.
  2. The value 94.0 is at least 90.0.
  3. Both macro results are true, so the combined result is true.

Your task

  1. Run the example.
  2. Change 72 to 42 and identify which macro fails.
  3. Restore it, change 94 to 84, and identify the other failure.

Expected observation: A score below 50 fails all(); removing the only score at or above 90 fails exists().

Common mistake

The variable score exists only inside each macro predicate. It is not available after the macro call.

Show the explanation

all() returns true; exists() returns false.

Knowledge check

What do [].all(x, x > 0) and [].exists(x, x > 0) return?

Key takeaway

Use all() for a universal requirement and exists() for at least one match; consider empty collections explicitly.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.