Array.filled
func Array.filled[T](n: int, v: T): Array[T]Array.filled returns a new array of n elements, each the same v. Every slot holds v itself, not a copy. It panics if n < 0 or n is above 2^31 - 1.
Details
- Panics when
nis below 0 or above 2^31 − 1. - When
vis an array, map, set, or record, every slot refers to that one value, so a change through one slot shows in all of them.
Basic examples
Make an array of three zeros:
Array.filled(3, 0) // [0, 0, 0]The result is a new array. Writing a slot changes only that slot:
let marks = Array.filled(4, "-")
marks[1] = "x"
marks // ["-", "x", "-", "-"]Scope
An n of 0 gives an empty array:
Array.filled(0, "a") // []v can be any value: a string, a float, a bool, or a record:
type Point = { x: int, y: int }
Array.filled(2, "ab") // ["ab", "ab"]
Array.filled(2, 1.5) // [1.5, 1.5]
Array.filled(3, false) // [false, false, false]
Array.filled(2, Point{ x: 0, y: 0 }) // [Point{ x: 0, y: 0 }, Point{ x: 0, y: 0 }]To fill with null and store values later, annotate the element type. The annotation makes the array an Array[int | null]:
let slots: Array[int | null] = Array.filled(3, null)
slots[0] = 5
slots // [5, null, null]The count comes first, so a pipe passes the count:
3 | Array.filled("z") // ["z", "z", "z"]The parameters are named n and v, and named arguments may come in any order:
Array.filled(n: 2, v: "x") // ["x", "x"]
Array.filled(v: "x", n: 2) // ["x", "x"]Applications
Count how often each digit occurs, with one counter per digit:
let counts = Array.filled(5, 0)
for d in [1, 3, 3, 4, 1, 1] {
counts[d] += 1
}
counts // [0, 3, 0, 2, 1]Pad an array of digits on the left to a fixed width:
func pad(xs: Array[int], width: int): Array[int] {
[...Array.filled(width - len(xs), 0), ...xs]
}
pad([7, 8], 5) // [0, 0, 0, 7, 8]Build a grid with a new row per line, then draw it:
let grid = Array.range(0, 3).map({ Array.filled(4, ".") })
grid[1][2] = "#"
for row in grid {
println(String.join(row, ""))
}....
..#.
....Give each goroutine its own slot to write a result into, then read the slots after the wait:
let words = ["ant", "bee", "wasp"]
let lengths: Array[int | null] = Array.filled(len(words), null)
let wg = WaitGroup.new()
for { index, value } in words.entries() {
wg.add(1)
go {
defer { wg.done() }
lengths[index] = value.length()
}
}
wg.wait()
println(lengths)[3, 3, 4]Parallel.map does this for a whole array.
Properties and relations
The result has n elements, and every one is v:
let xs = Array.filled(4, "a")
assert(len(xs) == 4)
assert(xs.every({ x == "a" }))It equals mapping every index of Array.range(0, n) to v:
assert(Array.filled(3, 1) == Array.range(0, 3).map({ 1 }))Joining n copies of a string gives the same string as String.repeat:
assert(String.join(Array.filled(3, "ab"), "") == "ab".repeat(3))Possible issues
A negative count panics. Code that computes the count, such as pad above, panics when the computation goes below 0:
Array.filled(-1, 0) // panic: Array.filled needs a count of at least 0, got -1A count above 2^31 − 1 panics too:
Array.filled(3000000000, 0) // panic: Array.filled needs a count of at most 2147483647, got 3000000000Every slot holds the same v, not a copy. When v is an array or a record, a change through one slot shows in every slot:
let rows = Array.filled(2, [0, 0])
rows[0][0] = 1
rows // [[1, 0], [1, 0]]
Array.same(rows[0], rows[1]) // trueBuild a new value per slot with Array.map over Array.range instead:
let rows = Array.range(0, 2).map({ [0, 0] })
rows[0][0] = 1
rows // [[1, 0], [0, 0]]Without an annotation, Array.filled(n, null) is an Array[null], which holds nothing but null:
let slots = Array.filled(3, null)
slots[0] = 5 // error: cannot use int where null is expectedAn empty array literal as v has no element type to infer, so annotate it:
let rows = Array.filled(2, [])
// error: cannot infer the element type of an empty array; annotate itSee also
Array.range, Array.map, Array.same, String.repeat, Array.push, Parallel.map
