Set.isSubset
func Set.isSubset[T](a: Set[T], b: Set[T]): boolSet.isSubset reports whether every member of a is a member of b.
Details
- Compares members by
==: arrays, maps, and sets match by contents, and records by tag and fields. - Neither set changes.
Basic examples
Set.isSubset(Set.of([1, 2]), Set.of([3, 2, 1])) // true
Set.isSubset(Set.of([1, 4]), Set.of([3, 2, 1])) // falseCall it through . on any set:
let picked = Set.of(["red", "blue"])
let palette = Set.of(["red", "green", "blue"])
picked.isSubset(palette) // true
palette.isSubset(picked) // falseScope
The empty set is a subset of every set, the empty set included. An inline Set.of([]) takes its element type from the other argument. When nothing gives it one, as when both sets are empty, annotate it:
Set.isSubset(Set.of([]), Set.of([1, 2])) // true
Set.of([1]).isSubset(Set.of([])) // false
let none: Set[int] = Set.of([])
none.isSubset(none) // trueA set is a subset of itself and of any equal set, whatever the order of its members:
let a = Set.of([1, 2, 3])
a.isSubset(a) // true
a.isSubset(Set.of([3, 2, 1])) // trueStrings compare by code points. == does not normalize, so the two spellings of "é" differ:
Set.of(["café"]).isSubset(Set.of(["café", "thé"])) // true
Set.of(["cafe\u{301}"]).isSubset(Set.of(["café"])) // falseRecords compare by tag and fields:
type Point = { x: int, y: int }
let corners = Set.of([Point{ x: 0, y: 0 }, Point{ x: 1, y: 1 }])
let grid = Set.of([Point{ x: 0, y: 0 }, Point{ x: 0, y: 1 }, Point{ x: 1, y: 0 }, Point{ x: 1, y: 1 }])
corners.isSubset(grid) // trueMembers of a tagged union compare by tag, so equal fields under another tag do not match:
type Circle = { size: float64 }
type Square = { size: float64 }
type Shape = Circle | Square
let drawn: Set[Shape] = Set.of([Circle{ size: 1.0 }, Square{ size: 2.0 }])
let small: Set[Shape] = Set.of([Circle{ size: 1.0 }])
let big: Set[Shape] = Set.of([Circle{ size: 2.0 }])
small.isSubset(drawn) // true
big.isSubset(drawn) // falseNested arrays and maps compare by contents:
Set.of([[1, 2]]).isSubset(Set.of([[1, 2], [3]])) // true
Set.of([Map{ a: 1 }]).isSubset(Set.of([Map{ a: 1 }])) // trueA set whose element type includes null can hold null like any other member:
let answers: Set[bool | null] = Set.of([true, null])
let unanswered: Set[bool | null] = Set.of([null])
unanswered.isSubset(answers) // true
answers.isSubset(Set.of([true, false])) // falseIn a pipeline, the candidate subset is the first argument:
"b a" | String.split(" ") | Set.of() | Set.isSubset(Set.of(["a", "b", "c"])) // trueApplications
Check that a request asks only for permissions a role grants:
func allowed(granted: Set[string], wanted: Array[string]): bool {
Set.of(wanted).isSubset(granted)
}
let editor = Set.of(["read", "write", "comment"])
println(allowed(editor, ["read", "write"]))
println(allowed(editor, ["read", "delete"]))true
falseReport the required columns a CSV header lacks, and accept it when there are none:
let required = Set.of(["id", "name", "email"])
let header = Set.of("id,email,age".split(","))
if required.isSubset(header) {
println("ok")
} else {
println("missing: ${required.difference(header).values()}")
}missing: ["name"]Test whether a word can be spelled from a set of letters, using each letter any number of times:
let letters = Set.of("aenst".chars())
for word in ["ants", "nest", "tent", "lens"] {
println("${word}: ${Set.of(word.chars()).isSubset(letters)}")
}ants: true
nest: true
tent: true
lens: false"tent" needs two "t"s and still passes: see Possible issues.
Properties and relations
a is a subset of b exactly when a has no member that b lacks, when their intersection is a, and when their union is b:
let b = Set.of([1, 2, 3, 4])
for a in [Set.of([2, 4]), Set.of([4, 5]), Set.of([1, 2, 3, 4])] {
let sub = a.isSubset(b)
assert(sub == Set.difference(a, b).isEmpty())
assert(sub == (Set.intersect(a, b) == a))
assert(sub == (Set.union(b, a) == b))
assert(sub == a.values().every({ b.has(x) }))
}Two sets are equal exactly when each is a subset of the other:
let a = Set.of(["x", "y"])
let b = Set.of(["y", "x"])
assert((a == b) == (a.isSubset(b) && b.isSubset(a)))Every set is a subset of its union with another, its intersection with another is a subset of it, and so is its difference with another:
let a = Set.of([1, 2])
let b = Set.of([2, 3])
assert(a.isSubset(Set.union(a, b)))
assert(Set.intersect(a, b).isSubset(a))
assert(Set.difference(a, b).isSubset(a))Possible issues
Set.isSubset allows the two sets to be equal. For a proper subset, also compare sizes:
let a = Set.of([1, 2])
let b = Set.of([2, 1])
a.isSubset(b) // true
a.isSubset(b) && a.size() < b.size() // falseNaN is not == to itself, so a set that holds NaN is not a subset even of itself:
let xs = Set.of([1.0, Math.NaN])
xs.isSubset(xs) // false
Set.of([1.0]).isSubset(xs) // trueThe first set fixes the element type. A set built from records of one member of a union is a set of that member, not of the union, and a set of the union is not assignable to it:
type Circle = { size: float64 }
type Square = { size: float64 }
type Shape = Circle | Square
let drawn: Set[Shape] = Set.of([Circle{ size: 1.0 }, Square{ size: 2.0 }])
println(Set.of([Circle{ size: 1.0 }]).isSubset(drawn))
// error: cannot use Set[Shape] where Set[Circle] is expectedAnnotate the first set with the union type, as in Scope above.
A set holds each value once, so isSubset ignores repeats:
Set.of("sees".chars()).isSubset(Set.of("se".chars())) // trueTo compare how many times each value occurs, count them in a Map.
Both sets have the same element type. Numeric types never mix:
let ids = Set.of([1, 2])
println(ids.isSubset(Set.of([1.0, 2.0]))) // error: cannot use float64 where int is expectedSet.isSubset asks about sets. To ask whether an array's elements all appear in another array, build a set from each, or test with Array.every:
let xs = [1, 2, 2]
let ys = [3, 2, 1]
Set.of(xs).isSubset(Set.of(ys)) // true
xs.every({ ys.contains(x) }) // trueThere is no Set.isSuperset. Swap the arguments: palette.isSuperset(picked) is picked.isSubset(palette):
let picked = Set.of(["red", "blue"])
let palette = Set.of(["red", "green", "blue"])
picked.isSubset(palette) // trueSee also
Set.has · Set.intersect · Set.difference · Set.union · Set.same · Set.of · Array.every · == operator
