Set.same
func Set.same[T](a: Set[T], b: Set[T]): boolSet.same reports whether a and b are the same set, not merely equal ones.
Details
==compares members in any order, so useSet.sameto tell a shared set from a copy.Set.of,Set.union,Set.intersect, andSet.differencealways build a new set.
Basic examples
A set is the same as itself, and as any other name for it:
let a = Set.of([1, 2])
let b = a
Set.same(a, a) // true
Set.same(a, b) // trueAn equal set built separately is not the same set:
let a = Set.of([1, 2])
a == Set.of([2, 1]) // true
Set.same(a, Set.of([2, 1])) // falseScope
Two empty sets are equal, and still two sets:
let a: Set[string] = Set.of([])
let b: Set[string] = Set.of([])
a == b // true
Set.same(a, b) // falseSet.union, Set.intersect, and Set.difference return a new set, even when the result has the same members as an argument:
let a = Set.of([1, 2])
let none: Set[int] = Set.of([])
Set.same(a, a.union(none)) // false
Set.same(a, a.intersect(a)) // false
Set.same(a, a.difference(none)) // falseSet.add and Set.remove change the set in place, so it stays the same set:
let a = Set.of(["x"])
let b = a
a.add("y") // true
a.remove("x") // true
Set.same(a, b) // true
b // Set.of(["y"])A set held in a record field is shared by a spread of the record, since the spread copies the field, which is a reference:
type User = { name: string, roles: Set[string] }
let u = User{ name: "ann", roles: Set.of(["admin"]) }
let v = User{ ...u, name: "bob" }
Set.same(u.roles, v.roles) // trueIn a pipeline, the left side is a:
let a = Set.of([1])
let b = a
a | Set.same(b) // trueCall it through . on any set:
let a = Set.of(["red"])
let b = a
a.same(b) // trueName the arguments, in any order:
let a = Set.of([1])
Set.same(a: a, b: a) // true
Set.same(b: a, a: Set.of([1])) // falseApplications
Guard a function that moves every member of one set into another against being given one set twice:
func moveAll(from: Set[string], to: Set[string]) {
if Set.same(from, to) {
return
}
for x in from.values() {
to.add(x)
from.remove(x)
}
}
let pending = Set.of(["a", "b"])
let done = Set.of(["z"])
moveAll(pending, done)
println("${pending} ${done}")
moveAll(done, done)
println(done)Set.of([]) Set.of(["z", "a", "b"])
Set.of(["z", "a", "b"])Properties and relations
Set.same is reflexive and symmetric:
let a = Set.of([1])
let b = a
assert(Set.same(a, a))
assert(Set.same(a, b) == Set.same(b, a))A set that holds a NaN is not == to itself, since floats compare by IEEE 754, but it is still the same set:
let a = Set.of([Math.NaN])
a == a // false
Set.same(a, a) // trueWhen two names refer to the same set, a change through one shows through the other. For a copy it does not:
let a = Set.of([1])
let b = a
let copy = Set.of(a.values())
a.add(2)
assert(Set.same(a, b) && b == Set.of([1, 2]))
assert(!Set.same(a, copy) && copy == Set.of([1]))Reflect.same reports the same for two reflected sets:
let a = Set.of([1])
let t = type(Set[int])
assert(Reflect.same(Reflect.valueOf(t, a), Reflect.valueOf(t, a)))
assert(!Reflect.same(Reflect.valueOf(t, a), Reflect.valueOf(t, Set.of([1]))))Possible issues
Both arguments have the one type Set[T]. A Set[int] and a Set[int | null] are different types, so comparing them is a compile error:
let a = Set.of([1])
let b: Set[int | null] = Set.of([1])
println(Set.same(a, b)) // error: cannot use Set[int | null] where Set[int] is expectedSee also
Array.same · Map.same · Record.same · Reflect.same · Set.of · Set.union · == operator
