How do I check if any or all items match a condition in EK9?

← Common Collection Tasks · Ref: Q185

EK9 uses stream pipelines with filter and empty checks to implement any-match and all-match patterns.

ANY MATCH

Check if at least one item matches by filtering and checking for non-empty results:

  matches <- cat items | filter by predicate | head 1 | collect as List of Integer
  if ~matches is empty
    stdout.println("At least one matches")

head 1 stops as soon as one match is found.

ALL MATCH

Check if ALL items match by filtering for the negation and checking for empty results:

  failures <- cat items | filter by negate | head 1 | collect as List of Integer
  if failures is empty
    stdout.println("All items match")

If no items fail the condition, then all items pass.

NONE MATCH

Check if NO items match:

  anyFound <- cat items | filter by predicate | head 1 | collect as List of Integer
  if anyFound is empty
    stdout.println("None match")

See Q89 for stream pipelines. See Q184 for finding the first match. See Q125 for head/tail/skip.

Example

defines module qa.collectiontasks.anyallmatch

  defines function

    isPositive() as pure
      -> num as Integer
      <- rtn as Boolean: num > 0

    isNotPositive() as pure
      -> num as Integer
      <- rtn as Boolean: num <= 0

    isEven() as pure
      -> num as Integer
      <- rtn as Boolean: num mod 2 == 0

    isNotEven() as pure
      -> num as Integer
      <- rtn as Boolean: num mod 2 <> 0

  defines program
    AnyAllMatchDemo()
      stdout <- Stdout()

      numbers <- [1, 2, 3, 4, 5]

      // === ANY MATCH ===

      // Check if any number is even
      anyEven <- cat numbers | filter by isEven | head 1 | collect as List of Integer
      if ~anyEven is empty
        stdout.println("At least one even number found")

      // === ALL MATCH ===

      // Check if ALL numbers are positive
      notPositive <- cat numbers | filter by isNotPositive | head 1 | collect as List of Integer
      if notPositive is empty
        stdout.println("All numbers are positive")

      // === NONE MATCH ===

      // Check if NONE are negative (all positive)
      anyNegative <- cat numbers | filter by isNotPositive | head 1 | collect as List of Integer
      if anyNegative is empty
        stdout.println("No negative numbers found")

      // === ALL EVEN CHECK (will fail) ===

      notEven <- cat numbers | filter by isNotEven | head 1 | collect as List of Integer
      if notEven is empty
        stdout.println("All even")
      else
        stdout.println("Not all numbers are even")

      // === ANY MATCH ON EMPTY LIST ===

      emptyList <- List() of Integer
      anyInEmpty <- cat emptyList | filter by isEven | head 1 | collect as List of Integer
      if anyInEmpty is empty
        stdout.println("Empty list: no matches (any returns false)")

Common mistakes

E50060 — Stdout does not have a display() method. The correct method is println(). Calling a non-existent method triggers E50060 — method not resolved. See ek9 -h E50060 for details.

Incorrect:

stdout.display("At least one even number found")

Correct:

stdout.println("At least one even number found")
Other ways to ask this
  • What is the EK9 equivalent of anyMatch() or allMatch()?
  • How do I check if every element satisfies a condition in EK9?
  • How do I check if at least one element matches in EK9?

Coming from another language?

Java: stream.anyMatch(pred), stream.allMatch(pred), stream.noneMatch(pred). Python: any(pred(x) for x in items), all(pred(x) for x in items). Rust: iter.any(pred), iter.all(pred). Go: manual loops. JavaScript: arr.some(pred), arr.every(pred). Kotlin: list.any(pred), list.all(pred), list.none(pred). EK9: filter + head 1 + empty check for any, filter by negation + empty check for all.

Keywords: some, filter, condition, task, all, any, match, check, collection, every, stream, none, predicate