Set.union
func Set.union[T](a: Set[T], b: Set[T]): Set[T]Set.union returns a new set holding the members of a, then those of b that a lacks.
Details
- Returns a new set. Neither argument changes, even when the other one is empty.
- The members that
badds follow in the order ofb. A member of both keeps its position ina.
Basic examples
Combine two sets; a member of both appears once:
Set.union(Set.of([1, 2]), Set.of([3, 2])) // Set.of([1, 2, 3])The members of a come first, and b adds only what a lacks:
Set.union(Set.of([3, 1]), Set.of([2, 1, 0])).values() // [3, 1, 2, 0]Scope
An empty set on either side gives the other one's members:
let none: Set[int] = Set.of([])
Set.union(none, Set.of([1])) // Set.of([1])
Set.union(Set.of([1]), none) // Set.of([1])
Set.union(none, none) // Set.of([])A set joined with itself has the same members:
let xs = Set.of([1, 2])
Set.union(xs, xs) // Set.of([1, 2])Records and arrays match by ==, and null can be a member like any other value:
type Point = { x: int, y: int }
let p = Point{ x: 1, y: 2 }
Set.union(Set.of([p]), Set.of([Point{ x: 1, y: 2 }])) // Set.of([Point{ x: 1, y: 2 }])
Set.union(Set.of([[1], [2]]), Set.of([[1]])) // Set.of([[1], [2]])
Set.union(Set.of([1, null]), Set.of([null, 2])) // Set.of([1, null, 2])An expected type fixes T, so sets of different member types can join into a union:
let mixed: Set[int | string] = Set.union(Set.of([1]), Set.of(["x"]))
mixed // Set.of([1, "x"])Call it through . on a set, or with a pipe, which passes its left side as the first argument:
Set.of([1]).union(Set.of([2])) // Set.of([1, 2])
Set.of([1]) | Set.union(Set.of([2])) // Set.of([1, 2])Name the arguments to pass them in any order:
Set.union(b: Set.of([2]), a: Set.of([1])) // Set.of([1, 2])Applications
List every tag used by two posts, each once, in the order they first appear:
let first = Set.of(["ant", "lang", "docs"])
let second = Set.of(["docs", "tests"])
println(Set.union(first, second).values() | String.join(", "))ant, lang, docs, testsCollect the members of several sets with a loop:
let groups = [Set.of(["ann", "bob"]), Set.of(["bob", "cy"]), Set.of(["ann", "dee"])]
var everyone: Set[string] = Set.of([])
for g in groups {
everyone = Set.union(everyone, g)
}
println(everyone.values())["ann", "bob", "cy", "dee"]Properties and relations
Set.union is commutative as sets, since == on sets ignores order, but the order of the members differs:
let a = Set.of([1, 2])
let b = Set.of([2, 3])
assert(Set.union(a, b) == Set.union(b, a))
assert(Set.union(a, b).values() != Set.union(b, a).values())Both arguments are subsets of the result:
let a = Set.of([1, 2])
let b = Set.of([2, 3])
let u = Set.union(a, b)
assert(Set.isSubset(a, u))
assert(Set.isSubset(b, u))Its size is the sum of the sizes less the members the two share:
let a = Set.of([1, 2, 3])
let b = Set.of([3, 4])
assert(Set.union(a, b).size() == a.size() + b.size() - a.intersect(b).size())The result equals Set.of over the values of a followed by the values of b:
let a = Set.of([3, 1])
let b = Set.of([1, 2])
assert(Set.union(a, b).values() == Set.of([...a.values(), ...b.values()]).values())The result is a new set, even when one side is empty, and adding to it leaves a as it was:
let a = Set.of([1, 2])
let u = Set.union(a, Set.of([]))
assert(u == a)
assert(!Set.same(u, a))
u.add(9) // true
a // Set.of([1, 2])
u // Set.of([1, 2, 9])Possible issues
Set.union does not change a. To add members to a set in place, call Set.add for each, or assign the result back:
var seen = Set.of([1])
Set.union(seen, Set.of([2])) // Set.of([1, 2])
seen // Set.of([1])
seen = Set.union(seen, Set.of([2]))
seen // Set.of([1, 2])Strings match by code point, so two spellings of the same text stay two members:
Set.union(Set.of(["é"]), Set.of(["é"])).size() // 2Two sets of different member types do not join without an expected type that holds both:
let both = Set.union(Set.of([1]), Set.of(["x"])) // error: cannot use string where int is expectedBoth arguments are sets. Turn an array into a set with Set.of first:
let more = Set.union(Set.of([1]), [2]) // error: cannot use Array[int] where Set[int] is expectedTwo empty sets built from literals leave T open. Annotate one side, or the result:
let none = Set.union(Set.of([]), Set.of([]))
// error: cannot infer the element type of an empty array; annotate itSee also
Set.intersect, Set.difference, Set.isSubset, Set.add, Set.of, Set.values, Map.merge, Array.concat
