Skip to content

Map#

The e.map(x, t) macro transforms each element x in collection e using expression t, producing a new list.

Lists#

Apply a transformation to each element.

[1, 2, 3].map(x, x * 2)
// result: [2, 4, 6] (list)
["a", "b", "c"].map(s, s + "!")
// result: ["a!", "b!", "c!"] (list)

Mapping an empty list returns an empty list.

[].map(x, x * 2)
// result: [] (list)

Maps#

When applied to a map type, map iterates over keys and produces a list.

// input: scores = {"alice": 95, "bob": 87}
scores.map(name,
  scores[name] + 5)
// result: [100, 92] (list)

An empty map returns an empty list.

{}.map(k, k + "!")
// result: [] (list)

Filter and transform#

The e.map(x, p, t) form filters before transforming. Only elements where predicate p is true are included in the result.

[1, 2, 3, 4, 5].map(x, x > 2, x * 10)
// result: [30, 40, 50] (list)

This is equivalent to chaining filter and map, but avoids allocating the result of the filter.

[1, 2, 3, 4, 5].filter(x, x > 2).map(x, x * 10)
// result: [30, 40, 50] (list)

See also#