Array.same
func Array.same[T](xs: Array[T], ys: Array[T]): boolArray.same reports whether xs and ys are the same array, not merely equal ones.
Details
- Returns
trueonly whenxsandysare one array, even when two different arrays hold==elements. ==compares elements, so useArray.sameto tell a shared array from a copy.
Basic examples
An array is the same as itself, and as any other name for it:
let xs = [1, 2]
let ys = xs
Array.same(xs, xs) // true
Array.same(xs, ys) // trueAn equal array built separately is not the same array:
let xs = [1, 2]
xs == [1, 2] // true
Array.same(xs, [1, 2]) // falseCall it through . on any array:
let xs = ["a"]
let ys = xs
xs.same(ys) // trueScope
Two empty arrays are equal, and still two arrays:
let a: Array[string] = []
let b: Array[string] = []
a == b // true
Array.same(a, b) // falseA spread copies the elements into a new array, and so does a library function that returns an array, such as Array.slice over the whole array or Array.reverse:
let xs = [3, 1, 2]
Array.same(xs, [...xs]) // false
Array.same(xs, xs.slice(0, len(xs))) // false
Array.same(xs, xs.reverse().reverse()) // falseA copy is shallow. The inner arrays of a copied grid are the same arrays as before:
let grid = [[1, 2], [3]]
let copy = [...grid]
Array.same(grid, copy) // false
Array.same(grid[0], copy[0]) // trueAn array held in a record field is shared by a spread of the record, since the spread copies the field, which is a reference:
type Post = { title: string, tags: Array[string] }
let p = Post{ title: "Ants", tags: ["insects"] }
let q = Post{ ...p, title: "More ants" }
Array.same(p.tags, q.tags) // trueAn array passed to a function is the same array inside it, so the function can change it:
func addOne(xs: Array[int]): Array[int] {
xs.push(1)
xs
}
let xs = [0]
Array.same(addOne(xs), xs) // true
xs // [0, 1]The arrays may hold any element type, such as records, or functions:
type Point = { x: int, y: int }
let path = [Point{ x: 0, y: 0 }]
let route = path
Array.same(path, route) // true
let fs: Array[func(int): int] = [{ x + 1 }]
Array.same(fs, fs) // trueIn a pipeline, the left side is xs:
let xs = [1]
let ys = xs
xs | Array.same(ys) // trueName the arguments, in any order:
let xs = [1]
Array.same(xs: xs, ys: xs) // true
Array.same(ys: xs, xs: [1]) // falseApplications
Find rows of a grid that share one array, as Array.filled gives when its value is an array, since it puts the same value in every slot:
func sharedRows(grid: Array[Array[string]]): bool {
for i in Array.range(0, len(grid)) {
for j in Array.range(i + 1, len(grid)) {
if Array.same(grid[i], grid[j]) {
return true
}
}
}
false
}
sharedRows(Array.filled(2, ["."])) // true
sharedRows(Array.range(0, 2).map({ ["."] })) // falseGuard a function that empties one array into another against being given one array twice:
func moveAll(from: Array[string], to: Array[string]) {
if Array.same(from, to) {
return
}
to.extend(from)
while from.pop() != null {}
}
let inbox = ["a", "b"]
let done = ["z"]
moveAll(inbox, done)
println("${inbox} ${done}")
moveAll(done, done)
println(done)[] ["z", "a", "b"]
["z", "a", "b"]Properties and relations
Array.same is reflexive and symmetric:
let xs = [1, 2]
let ys = xs
assert(Array.same(xs, xs))
assert(Array.same(xs, ys) == Array.same(ys, xs))The same array is == to itself unless it holds a NaN, since floats compare with IEEE 754 rules:
let xs = [Math.NaN]
xs == xs // false
Array.same(xs, xs) // trueWhen two names refer to the same array, a change through one shows through the other. For a copy it does not:
let xs = [1]
let ys = xs
let zs = [...xs]
xs.push(2)
assert(Array.same(xs, ys) && ys == [1, 2])
assert(!Array.same(xs, zs) && zs == [1])Reflect.same reports the same for two reflected arrays:
let xs = [1]
let t = type(Array[int])
assert(Reflect.same(Reflect.valueOf(t, xs), Reflect.valueOf(t, xs)))
assert(!Reflect.same(Reflect.valueOf(t, xs), Reflect.valueOf(t, [1])))Possible issues
== compares contents, so it cannot tell whether two names share an array. Use Array.same when that is the question:
let xs = [1]
let copy = [...xs]
xs == copy // true
Array.same(xs, copy) // falseBoth arguments have the one type Array[T]. An Array[int] and an Array[int | null] are different types, so comparing them is a compile error:
let xs = [1]
let ys: Array[int | null] = [1]
println(Array.same(xs, ys)) // error: cannot use Array[int | null] where Array[int] is expectedA nullable array, such as a value read from a map, must be narrowed first:
let xs = [1]
let m = Map{ a: xs }
println(Array.same(m["a"], xs)) // error: cannot use Array[int] | null where Array[T] is expectedlet xs = [1]
let m = Map{ a: xs }
let found = m["a"]
found != null && Array.same(found, xs) // trueSee also
Record.same · Map.same · Set.same · Reflect.same · Array.filled · Array.slice · == operator
