Map.same
func Map.same[K, V](m: Map[K, V], n: Map[K, V]): boolMap.same reports whether m and n are the same map, not merely equal ones.
Details
==compares keys and values, so useMap.sameto tell a shared map from a copy.
Basic examples
A map is the same as itself, and as any other name for it:
let m = Map{ a: 1 }
let n = m
Map.same(m, m) // true
Map.same(m, n) // trueAn equal map built separately is not the same map:
let m = Map{ a: 1 }
m == Map{ a: 1 } // true
Map.same(m, Map{ a: 1 }) // falseCall it through . on any map:
let m = Map{ a: 1 }
let n = m
m.same(n) // trueScope
Two empty maps are equal, and still two maps:
let a: Map[string, int] = Map{}
let b: Map[string, int] = Map{}
a == b // true
Map.same(a, b) // falseMap.filter, Map.map, Map.merge, and Map.fromEntries build a new map, even when nothing changes:
let m = Map{ a: 1, b: 2 }
Map.same(m, m.filter({ true })) // false
Map.same(m, m.merge(Map{})) // false
Map.same(m, Map.fromEntries(m.entries())) // falseA map held in a record field is shared by a spread of the record, since the spread copies the field, which is a reference. A field default builds a new map at every literal:
type Config = { host: string, headers: Map[string, string] = Map{} }
let c = Config{ host: "a.example" }
let d = Config{ ...c, host: "b.example" }
let e = Config{ host: "a.example" }
Map.same(c.headers, d.headers) // true
Map.same(c.headers, e.headers) // falseA map passed to a function is the same map inside it, so the function can change it:
func count(m: Map[string, int], word: string): Map[string, int] {
m[word] = (m[word] ?? 0) + 1
m
}
let words: Map[string, int] = Map{}
Map.same(count(words, "ant"), words) // true
words // Map{ "ant": 1 }The maps may have any key and value types, such as records as keys, or arrays as values:
type Point = { x: int, y: int }
let names = Map{ (Point{ x: 0, y: 0 }): "origin" }
let labels = names
Map.same(names, labels) // true
let groups = Map{ odd: [1, 3], even: [2] }
Map.same(groups, groups) // trueIn a pipeline, the left side is m:
let m = Map{ a: 1 }
let n = m
m | Map.same(n) // trueName the arguments, in any order:
let m = Map{ a: 1 }
Map.same(m: m, n: m) // true
Map.same(n: m, m: Map{ a: 1 }) // falseApplications
Guard a function that moves every entry of one map into another against being given one map twice:
func moveAll(from: Map[string, int], to: Map[string, int]) {
if Map.same(from, to) {
return
}
for e in from {
to[e.key] = e.value
}
for k in from.keys() {
from.remove(k)
}
}
let pending = Map{ a: 1, b: 2 }
let stored = Map{ z: 0 }
moveAll(pending, stored)
println("${pending} ${stored}")
moveAll(stored, stored)
println(stored)Map{} Map{ "z": 0, "a": 1, "b": 2 }
Map{ "z": 0, "a": 1, "b": 2 }Properties and relations
Map.same is reflexive and symmetric:
let m = Map{ a: 1 }
let n = m
assert(Map.same(m, m))
assert(Map.same(m, n) == Map.same(n, m))The same map is == to itself unless it holds a NaN, since floats compare with IEEE 754 rules:
let m = Map{ x: Math.NaN }
m == m // false
Map.same(m, m) // trueWhen two names refer to the same map, a change through one shows through the other. For a copy it does not:
let m = Map{ a: 1 }
let n = m
let copy = Map.fromEntries(m.entries())
m["b"] = 2
assert(Map.same(m, n) && n == Map{ a: 1, b: 2 })
assert(!Map.same(m, copy) && copy == Map{ a: 1 })Reflect.same reports the same for two reflected maps:
let m = Map{ a: 1 }
let t = type(Map[string, int])
assert(Reflect.same(Reflect.valueOf(t, m), Reflect.valueOf(t, m)))
assert(!Reflect.same(Reflect.valueOf(t, m), Reflect.valueOf(t, Map{ a: 1 })))Possible issues
== compares contents, so it cannot tell whether two names share a map. Use Map.same when that is the question:
let m = Map{ a: 1 }
let copy = m.merge(Map{})
m == copy // true
Map.same(m, copy) // falseBoth arguments have the one type Map[K, V]. A Map[string, int] and a Map[string, int | null] are different types, so comparing them is a compile error:
let m = Map{ a: 1 }
let n: Map[string, int | null] = Map{ a: 1 }
println(Map.same(m, n))
// error: cannot use Map[string, int | null] where Map[string, int] is expectedA nullable map, such as a value read from another map, must be narrowed first:
let m = Map{ a: 1 }
let nested = Map{ inner: m }
println(Map.same(nested["inner"], m))
// error: cannot use Map[string, int] | null where Map[K, V] is expectedlet m = Map{ a: 1 }
let nested = Map{ inner: m }
let found = nested["inner"]
found != null && Map.same(found, m) // trueSee also
Array.same · Record.same · Set.same · Reflect.same · Map.merge · Map.fromEntries · == operator
