Skip to content

Exists one#

The e.exists_one(x, p) macro tests whether predicate p is true for exactly one element x in collection e.

Lists#

Returns true if the predicate is true for exactly one element.

[1, 2, 2].exists_one(x, x == 1)
// result: true (bool)
[1, 2, 2].exists_one(x, x == 2)
// result: false (bool)
[1, 2, 2].exists_one(x, x == 3)
// result: false (bool)

An empty list returns false.

[].exists_one(x, x > 0)
// result: false (bool)

Maps#

When applied to a map, exists_one iterates over the keys.

// input: scores = {"alice": 95, "bob": 72}
scores.exists_one(name, scores[name] >= 90)
// result: true (bool)
// input: scores = {"alice": 95, "bob": 92}
scores.exists_one(name, scores[name] >= 90)
// result: false (bool)

An empty map returns false.

{}.exists_one(k, k == "x")
// result: false (bool)

See also#

  • Exists - Existential quantifier
  • All - Universal quantifier