Map.entries
func Map.entries[K, V](m: Map[K, V]): Array[Entry[K, V]]Map.entries returns the entries of m in insertion order.
Details
- Replacing a value keeps its entry's position; a key removed and inserted again goes to the end.
Map.entriesreturns a new array, so later changes tomdo not change it, and changes to it do not changem. Eachvalueis the stored value itself, not a copy.- Each
Entryis a new record, so assigning to its fields does not changem.
Basic examples
List the entries of a map:
let ages = Map{ ann: 31, bob: 27 }
Map.entries(ages) // [Entry{ key: "ann", value: 31 }, Entry{ key: "bob", value: 27 }]Read the fields of one entry:
let es = Map{ red: "#f00", green: "#0f0" }.entries()
es[1].key // "green"
es[1].value // "#0f0"Loop over the entries and destructure each one:
let stock = Map{ apples: 3, pears: 0 }
for { key, value } in stock.entries() {
println("${key}: ${value}")
}apples: 3
pears: 0Scope
An empty map has no entries. Annotate it so that K and V are known:
let empty: Map[string, int] = Map{}
empty.entries() // []The order is the order keys were first inserted. Replacing a value keeps its place; a key removed and inserted again goes to the end:
let m = Map{ a: 1, b: 2, c: 3 }
m["a"] = 10
m.remove("b") // 2
m["b"] = 20
m.entries() // [Entry{ key: "a", value: 10 }, Entry{ key: "c", value: 3 }, Entry{ key: "b", value: 20 }]Keys of any type, records included, and null values come through as they are:
type Point = { x: int, y: int }
Map{ 1: "one", 2: "two" }.entries() // [Entry{ key: 1, value: "one" }, Entry{ key: 2, value: "two" }]
Map{ (Point{ x: 1, y: 2 }): "a" }.entries() // [Entry{ key: Point{ x: 1, y: 2 }, value: "a" }]
let maybe: Map[string, int | null] = Map{ a: null }
maybe.entries() // [Entry{ key: "a", value: null }]Call it through . or in a pipe:
let m = Map{ x: 1 }
m.entries() // [Entry{ key: "x", value: 1 }]
m | Map.entries() // [Entry{ key: "x", value: 1 }]Rename the fields as you destructure them:
let ages = Map{ ann: 31, bob: 27 }
for { key: name, value: age } in ages.entries() {
println("${name} is ${age}")
}ann is 31
bob is 27Applications
Find the key with the largest value:
let votes = Map{ tea: 3, coffee: 7, water: 5 }
votes.entries().sortBy({ -x.value })[0].key // "coffee"Build a query string:
let params = Map{ q: "ants", page: "2" }
params.entries().map({ "${x.key}=${x.value}" }) | String.join("&") // "q=ants&page=2"Sort a map by its keys:
let m = Map{ b: 2, c: 3, a: 1 }
m.entries().sortBy({ x.key }) | Map.fromEntries() // Map{ "a": 1, "b": 2, "c": 3 }Swap keys and values:
let codes = Map{ ok: 200, missing: 404 }
codes.entries().map({ Entry{ key: x.value, value: x.key } }) | Map.fromEntries() // Map{ 200: "ok", 404: "missing" }Remove entries while you walk the map. The array is taken before the loop starts, so removing from m does not disturb it:
let sessions = Map{ s1: 0, s2: 5, s3: 0 }
for { key, value } in sessions.entries() {
if value == 0 {
sessions.remove(key)
}
}
println(sessions)Map{ "s2": 5 }Properties and relations
The keys and the values of the entries are Map.keys and Map.values, and Map.fromEntries turns the entries back into an equal map:
let m = Map{ a: 1, b: 2, c: 3 }
let es = m.entries()
assert(len(es) == m.size())
assert(es.map({ x.key }) == m.keys())
assert(es.map({ x.value }) == m.values())
assert(Map.fromEntries(es) == m)
assert(es.every({ m[x.key] == x.value }))for over the map itself visits the same entries in the same order:
let m = Map{ x: 1, y: 2 }
let seen: Array[Entry[string, int]] = []
for e in m {
seen.push(e)
}
assert(seen == m.entries())The result is a new array, so inserting into m afterwards leaves it as it was:
let m = Map{ a: 1 }
let es = m.entries()
m["b"] = 2
assert(len(es) == 1)Possible issues
The fields are key and value. Array.entries gives Indexed records with index and value, so a destructuring written for an array does not fit:
let ages = Map{ ann: 31 }
for { index, value } in ages.entries() { // error: Entry[string, int] has no field index
println("${index}: ${value}")
}An entry holds the same value as the map, so a change made to a stored array shows in the entry:
let lists = Map{ a: [1] }
let es = lists.entries()
lists["a"]?.push(2)
es[0].value // [1, 2]The other way round, assigning an entry's value does not change the map. Write m[e.key] = ... to change it:
let m = Map{ a: 1 }
let es = m.entries()
es[0].value = 9
m // Map{ "a": 1 }The same holds for the entries a for loop over the map visits:
let m = Map{ a: 1, b: 2 }
for e in m {
e.value = 0
}
println(m)Map{ "a": 1, "b": 2 }Map.entries takes a map, not a set:
println(Map.entries(Set.of([1]))) // error: cannot use Set[int] where Map[K, V] is expectedSee also
Entry · Map.keys · Map.values · Map.fromEntries · Map.map · Map.filter · Array.entries
