Set.intersect
func Set.intersect[T](a: Set[T], b: Set[T]): Set[T]Set.intersect returns a new set holding the members of a that b has, in the order of a.
Details
- Returns a new set; neither
anorbchanges, even whenbhas every member ofa. - The order of
bdoes not matter.
Basic examples
Keep the numbers that both sets hold:
Set.intersect(Set.of([1, 2, 3, 4]), Set.of([3, 4, 5])) // Set.of([3, 4])The result follows the order of the first set:
Set.intersect(Set.of(["c", "a", "b"]), Set.of(["b", "c", "x"])) // Set.of(["c", "b"])Scope
An empty set on either side, or two sets with no member in common, give the empty set:
let none: Set[int] = Set.of([])
Set.intersect(none, Set.of([1, 2])) // Set.of([])
Set.intersect(Set.of([1, 2]), none) // Set.of([])
Set.intersect(Set.of([1, 2]), Set.of([3])) // Set.of([])Records match by their fields, not by identity:
type Point = { x: int, y: int }
let visited = Set.of([Point{ x: 0, y: 0 }, Point{ x: 1, y: 2 }])
let targets = Set.of([Point{ x: 1, y: 2 }, Point{ x: 5, y: 5 }])
Set.intersect(visited, targets) // Set.of([Point{ x: 1, y: 2 }])null is a member like any other value:
let a: Set[string | null] = Set.of(["a", null])
let b: Set[string | null] = Set.of([null, "b"])
Set.intersect(a, b) // Set.of([null])Call it through . or in a pipe:
Set.of([1, 2, 3]).intersect(Set.of([2])) // Set.of([2])
Set.of([1, 2, 3]) | Set.intersect(Set.of([3, 1])) // Set.of([1, 3])Applications
Find the words two sentences share:
let first = Set.of("the quick brown fox".fields())
let second = Set.of("the lazy brown dog".fields())
Set.intersect(first, second) // Set.of(["the", "brown"])Intersect any number of sets by folding with Array.reduce:
let sets = [Set.of([1, 2, 3, 4]), Set.of([2, 3, 4]), Set.of([4, 3, 9])]
sets.reduce(sets[0], Set.intersect) // Set.of([3, 4])Keep only the permissions a role grants:
let grants = Set.of(["read", "write"])
let asked = Set.of(["read", "delete", "write"])
let allowed = Set.intersect(asked, grants)
allowed // Set.of(["read", "write"])Check whether two sets share a member:
!Set.intersect(Set.of([1, 2]), Set.of([2, 3])).isEmpty() // true
!Set.intersect(Set.of([1, 2]), Set.of([3, 4])).isEmpty() // falseProperties and relations
The result is a subset of both sets:
let a = Set.of([1, 2, 3, 4])
let b = Set.of([2, 4, 6])
let both = Set.intersect(a, b)
assert(both.isSubset(a))
assert(both.isSubset(b))Swapping the arguments gives an equal set, since == ignores order, but the members come in the order of the new first argument:
let a = Set.of([3, 1, 2])
let b = Set.of([1, 3])
Set.intersect(a, b) == Set.intersect(b, a) // true
Set.intersect(a, b).values() // [3, 1]
Set.intersect(b, a).values() // [1, 3]Set.intersect keeps the members of a that Set.difference(a, b) removes:
let a = Set.of(["x", "y", "z"])
let b = Set.of(["y", "w"])
assert(Set.intersect(a, b) == Set.difference(a, Set.difference(a, b)))The result is never larger than either set:
let a = Set.of([1, 2, 3, 4, 5])
let b = Set.of([4, 5, 6])
let n = Set.intersect(a, b).size()
assert(n <= a.size() && n <= b.size())The result is a new set even when it equals a, so adding to it leaves a as it was:
let a = Set.of([1, 2])
let same = Set.intersect(a, a)
assert(same == a)
assert(!Set.same(same, a))
same.add(9) // true
a // Set.of([1, 2])Possible issues
Set.intersect does not change a. A call whose result is thrown away keeps every member; assign the result:
let tags = Set.of(["red", "green", "blue"])
Set.intersect(tags, Set.of(["red"]))
println(tags)Set.of(["red", "green", "blue"])Both arguments must be sets. Turn an array into a set with Set.of first:
let ids = Set.of([1, 2, 3])
let kept = Set.intersect(ids, [2, 3]) // error: cannot use Array[int] where Set[int] is expectedBoth sets must have the same element type. A Set[int] is not a Set[int | null]:
let a: Set[int | null] = Set.of([1, null])
let b: Set[int] = Set.of([1])
let both = Set.intersect(a, b) // error: cannot use Set[int] where Set[int | null] is expectedSee also
Set.union, Set.difference, Set.isSubset, Set.has, Set.of, Array.filter
