Array.entries
func Array.entries[T](xs: Array[T]): Array[Indexed[T]]Array.entries returns each element of xs paired with its index, in order.
Details
- Indices start at 0, so an empty
xsgives[]. - Returns a new array, so later pushes or removals on
xsdo not change it. Eachvalueis the element itself, not a copy.
Basic examples
Pair each element with its index:
Array.entries(["a", "b", "c"]) // [Indexed{ index: 0, value: "a" }, Indexed{ index: 1, value: "b" }, Indexed{ index: 2, value: "c" }]Read the fields of one entry:
let es = Array.entries([10, 20, 30])
es[2].index // 2
es[2].value // 30Loop over the entries and destructure each one:
for { index, value } in ["ant", "bee"].entries() {
println("${index}: ${value}")
}0: ant
1: beeScope
An empty array has no entries. Annotate it so that T is known:
let empty: Array[int] = []
empty.entries() // []null elements get an index like any other:
["x", null].entries() // [Indexed{ index: 0, value: "x" }, Indexed{ index: 1, value: null }]Records, nested arrays, and union members are carried as they are:
type Point = { x: int, y: int }
[Point{ x: 1, y: 2 }].entries() // [Indexed{ index: 0, value: Point{ x: 1, y: 2 } }]
[[1], []].entries() // [Indexed{ index: 0, value: [1] }, Indexed{ index: 1, value: [] }]
let r: Array[Result[int, string]] = [Result.ok(1), Result.err("no")]
r.entries()[1] // Indexed{ index: 1, value: Err{ error: "no" } }A string is not an array. Split it into graphemes first:
"🐜🐝".graphemes().entries() // [Indexed{ index: 0, value: "🐜" }, Indexed{ index: 1, value: "🐝" }]Call it through . or in a pipe:
[1, 2].entries() // [Indexed{ index: 0, value: 1 }, Indexed{ index: 1, value: 2 }]
[1, 2] | Array.entries() // [Indexed{ index: 0, value: 1 }, Indexed{ index: 1, value: 2 }]Rename the fields as you destructure them:
for { index: rank, value: name } in ["Ann", "Bob"].entries() {
println("#${rank + 1} ${name}")
}#1 Ann
#2 BobIt works in a generic function, where the element type is a type parameter:
func everyOther[T](items: Array[T]): Array[T] {
items.entries().filter({ x.index % 2 == 0 }).map({ x.value })
}
println(everyOther(["a", "b", "c", "d", "e"]))
println(everyOther([1, 2]))["a", "c", "e"]
[1]Applications
Find the positions of the elements a test accepts:
let temps = [3, 8, 2, 9, 7]
temps.entries().filter({ x.value > 5 }).map({ x.index }) // [1, 3, 4]Weight each element by its position:
[4, 5, 6].entries().map({ x.index * x.value }) // [0, 5, 12]Find the first position of the largest element, together with its value (Array.maxBy keeps the first of equal keys):
let scores = [70, 95, 88, 95]
scores.entries().maxBy({ x.value }) // Indexed{ index: 1, value: 95 }Build a map from each name to its position:
let names = ["Ann", "Bob", "Cid"]
let pairs = names.entries().map({ Entry{ key: x.value, value: x.index } })
Map.fromEntries(pairs) // Map{ "Ann": 0, "Bob": 1, "Cid": 2 }Compare each element with the one before it:
let prices = [10, 12, 11, 15]
for { index, value } in prices.entries() {
if index > 0 && value > prices[index - 1] {
println("day ${index}: up ${value - prices[index - 1]}")
}
}day 1: up 2
day 3: up 4Properties and relations
There is one entry per element, the values are xs, and the indices are Array.range(0, len(xs)):
let xs = ["p", "q", "r"]
let es = xs.entries()
assert(len(es) == len(xs))
assert(es.map({ x.value }) == xs)
assert(es.map({ x.index }) == Array.range(0, len(xs)))
assert(es.every({ xs[x.index] == x.value }))The result is a new array, so pushing to xs afterwards leaves it as it was:
let xs = [1, 2, 3]
let es = xs.entries()
xs.push(4)
assert(len(es) == 3)An entry is an ordinary record and compares by == with one you write:
assert(["a"].entries()[0] == Indexed{ index: 0, value: "a" })Possible issues
The fields are index and value. A map's entries are Entry records with key and value, so a destructuring written for a map does not fit:
for { key, value } in ["a", "b"].entries() { // error: Indexed[string] has no field key
println("${key}: ${value}")
}Indices start at 0. Add 1 when you number lines for a reader:
["first", "second"].entries().map({ "${x.index + 1}. ${x.value}" }) // ["1. first", "2. second"]An entry holds the same record as xs, so a change made through xs shows in the entry:
type P = { x: int }
let xs = [P{ x: 1 }]
let es = xs.entries()
xs[0].x = 9
es[0].value.x // 9Copy the element first, for example with a record spread, if the entry must stay unchanged.
The other way round, assigning an entry's value does not change xs:
let xs = [5]
let es = xs.entries()
es[0].value = 7
xs // [5]An entry is a new record, so assigning its value replaces the field in the entry only. To change the array, write xs[e.index] = ....
Array.entries takes an array, not a string:
println(Array.entries("abc")) // error: cannot use string where Array[T] is expectedSee also
Indexed · Array.range · Array.map · Array.findIndex · Array.indexOf · Map.entries · Map.fromEntries · Iter.fromArray
