CH07-L02 · applied

Filter, map, and membership

Exclude values that are already registered and turn the remaining objects into a simple list.

General CELRuns hereDocumented
What will I learn?

You can combine negation, the in operator, filter(), and map() into one selection rule.

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

Combine the in operator, logical negation, filter(), and map() into one general pattern. The result contains names that are not yet present in a second list.

How it works

users
  .filter(user, !(user.name in registeredNames))
  .map(user, user.name)

The expression user.name in registeredNames tests membership. The outer ! reverses that result, so the filter keeps only users whose name is absent. The following map() projects names from those objects.

Evaluation step by step

  1. Maya in ['Maya', 'Leo'] is true, then negation makes it false.
  2. Nora in ['Maya', 'Leo'] is false, then negation makes it true.
  3. Leo is registered, so the filter removes him.
  4. The projection returns ['Nora'].

This pattern is useful whenever values listed in one collection must be excluded from another collection.

Your task

  1. Run the prepared expression.
  2. Remove ! and compare the result.
  3. Add Nora to registeredNames.

Expected observation: Without negation, the expression returns registered names. Adding Nora makes the original exclusion result empty.

Common mistake

Do not confuse !(value in list) with !value in list. Parentheses state clearly that the membership result is being negated.

Show the explanation

Membership is evaluated first, and then its boolean result is reversed. Parentheses also make that intention clear to the next reader.

Knowledge check

Why is it useful to filter the objects before map() turns them into names?

Key takeaway

Combining filter() and map() separates the decision about which items survive from the decision about their output shape.

Sources

  • CEL-DEV — official overview of CEL operators and collection macros.
  • CEL-LANG — official definition of in, negation, and macros.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.