Skip to content

Filter#

The e.filter(x, p) macro creates a new list containing only elements x from collection e where predicate p is true.

Lists#

Returns elements for which the predicate is true.

[1, 2, 3, 4, 5].filter(x, x > 2)
// result: [3, 4, 5] (list)
["apple", "banana", "cherry"].filter(s, s.startsWith("b"))
// result: ["banana"] (list)

Returns an empty list if no elements match.

[1, 2, 3].filter(x, x > 10)
// result: [] (list)

Filtering an empty list returns an empty list.

[].filter(x, x > 0)
// result: [] (list)

Maps#

When applied to a map, filter returns a list of keys that satisfy the predicate.

{"a": 1, "b": 2, "c": 3}.filter(k, k != "b")
// result: ["a", "c"] (list)

To filter by value, access the map within the predicate.

// input: scores = {"alice": 95, "bob": 72, "carol": 88}
scores.filter(name, scores[name] >= 80)
// result: ["alice", "carol"] (list)

An empty map returns an empty list.

{}.filter(k, k == "x")
// result: [] (list)

See also#

  • Lists - List basics
  • All - Universal quantifier
  • Exists - Existential quantifier