Array.push
func Array.push[T](xs: Array[T], v: T): voidArray.push appends v to the end of xs. It mutates xs.
Details
- Appends
vtoxsin place and returnsvoid. Every name that shares the array sees the new element. - A
forloop overxsvisits elements pushed during the loop.
Basic examples
Append a value to the end of an array:
let xs = [1, 2]
Array.push(xs, 3)
xs // [1, 2, 3]Grow an empty array one element at a time. An empty literal needs its element type:
let names: Array[string] = []
Array.push(names, "Ada")
Array.push(names, "Linus")
names // ["Ada", "Linus"]Scope
Records, null in an array that allows it, and arrays inside arrays push alike:
type Point = { x: int, y: int }
let points = [Point{ x: 1, y: 2 }]
points.push(Point{ x: 3, y: 4 })
points // [Point{ x: 1, y: 2 }, Point{ x: 3, y: 4 }]
let maybe: Array[int | null] = [1]
maybe.push(null)
maybe // [1, null]
let rows = [[1, 2]]
rows.push([3])
rows // [[1, 2], [3]]A let array can be pushed to, since the binding stays the same:
let stack = ["a"]
stack.push("b")
stack // ["a", "b"]Call it through . on an array, or with a pipe, which passes its left side as the first argument:
let xs = [1]
xs.push(2)
xs | Array.push(3)
xs // [1, 2, 3]Name the arguments, in any order:
let xs = [1]
Array.push(v: 2, xs: xs)
xs // [1, 2]Applications
Collect the results of a loop that keeps state, here the running total:
let totals: Array[int] = []
var sum = 0
for n in [3, 1, 4, 1, 5] {
sum += n
totals.push(sum)
}
totals // [3, 4, 8, 9, 14]Use an array as a stack with Array.pop, here to check that brackets balance:
func balanced(s: string): bool {
let open: Array[string] = []
for c in s {
if c == "(" || c == "[" {
open.push(c)
} else if c == ")" || c == "]" {
let want = if c == ")" { "(" } else { "[" }
if open.pop() != want {
return false
}
}
}
open.isEmpty()
}
balanced("([]())") // true
balanced("([)]") // false
balanced("((") // falseVisit a tree breadth-first. The for loop reaches the nodes pushed while it runs:
type Node = { name: string, children: Array[Node] }
let tree = Node{
name: "root",
children: [
Node{ name: "a", children: [Node{ name: "a1", children: [] }] },
Node{ name: "b", children: [] },
],
}
let queue = [tree]
for node in queue {
println(node.name)
for child in node.children {
queue.push(child)
}
}root
a
b
a1Push from several goroutines under a Mutex, which keeps the pushes from racing:
let results: Array[int] = []
let mu = Mutex.new()
let wg = WaitGroup.new()
for n in [1, 2, 3, 4] {
wg.add(1)
go {
defer { wg.done() }
mu.withLock({ results.push(n * n) })
}
}
wg.wait()
println(results.sort())[1, 4, 9, 16]Properties and relations
The length grows by one, and the last element is v:
let xs = [1, 2]
let before = xs.length()
xs.push(7)
assert(xs.length() == before + 1)
assert(xs.last() == 7)Array.pop undoes it:
let xs = [1, 2]
xs.push(3)
assert(xs.pop() == 3)
assert(xs == [1, 2])It does in place what the spread literal [...xs, v] does in a new array:
let xs = [1, 2]
let copy = [...xs, 3]
xs.push(3)
assert(xs == copy)It is Array.insert at the length, and Array.extend with one element:
let a = [1]
let b = [1]
let c = [1]
a.push(2)
b.insert(b.length(), 2)
c.extend([2])
assert(a == b && b == c)Every other name for the array sees the push:
let xs = [1]
let alias = xs
xs.push(2)
assert(Array.same(alias, xs))
alias // [1, 2]Possible issues
Array.push returns void, not the array, so its result cannot be assigned back:
var xs = [1]
xs = xs.push(2) // error: cannot use void where Array[int] is expectedTo get a new array and keep the old one, use the spread literal:
let xs = [1]
let ys = [...xs, 2]
xs // [1]
ys // [1, 2]v must have the element type. An array is one value, so pushing it adds one element; to append its elements, use Array.extend:
let xs = [1]
xs.push([2, 3]) // error: cannot use Array[int] where int is expectedlet xs = [1]
xs.extend([2, 3])
xs // [1, 2, 3]An empty literal has no element type to infer, so annotate the variable:
let xs = [] // error: cannot infer the element type of an empty array; annotate it
xs.push(1)The value is pushed, not copied. An array or a record pushed in is the same value in the array, so a later change to it shows through both:
let row = [0]
let grid: Array[Array[int]] = []
grid.push(row)
row[0] = 9
grid // [[9]]A for loop over the array visits what the body pushes, so a loop that always pushes never ends. Loop over a copy, or push to another array:
let xs = [1, 2]
for x in [...xs] {
xs.push(x * 10)
}
xs // [1, 2, 10, 20]A function that takes an array and a callback reads the elements once, when it is called, while a for loop reads the array as it goes. A callback that pushes to the array it was given does not see its own pushes:
let a = [1, 2, 3]
a.map({ a.push(x + 10); x * 2 }) // [2, 4, 6]
a // [1, 2, 3, 11, 12, 13]See also
Array.pop, Array.extend, Array.insert, Array.concat, Array.last, Array.same
