Set.size
func Set.size[T](s: Set[T]): intSet.size returns the number of members of s.
Details
- Members compare the way
==does, so two equal records or arrays are one member. len(s)returns the same count.
Basic examples
Set.size(Set.of(["red", "green", "blue"])) // 3
let none: Set[int] = Set.of([])
Set.size(none) // 0Call it through . on any set:
let tags = Set.of(["go", "ant"])
tags.size() // 2Scope
Set.of keeps one of each == value, so the size counts distinct values, not array elements:
Set.of([1, 2, 2, 3, 3, 3]).size() // 3Equal records are one member:
type Point = { x: int, y: int }
let points = Set.of([Point{ x: 1, y: 2 }, Point{ x: 1, y: 2 }, Point{ x: 0, y: 0 }])
points.size() // 2null is a member like any other:
let answers: Set[bool | null] = Set.of([true, null, null])
answers.size() // 2Strings compare by code points, without normalizing, so two spellings of one letter are two members:
Set.of(["café", "cafe\u{301}"]).size() // 2The size follows the set as it changes:
let seen: Set[string] = Set.of([])
seen.add("go") // true
seen.add("ant") // true
seen.size() // 2
seen.add("go") // false
seen.size() // 2
seen.remove("go") // true
seen.size() // 1
seen.remove("zig") // false
seen.size() // 1In a pipeline, the set is the first argument:
Set.of([1, 2]) | Set.size() // 2As a function value, it maps an array of sets to their sizes:
[Set.of([1]), Set.of([1, 2])].map(Set.size) // [1, 2]For a set that may be null, ?. yields null instead of calling:
let tags: Set[string] | null = null
tags?.size() // null
tags?.size() ?? 0 // 0Applications
Count the distinct words of a text:
func distinct(text: string): int {
Set.of(text.fields()).size()
}
println(distinct("to be or not to be"))4Report whether a list has duplicates:
func hasDuplicates(xs: Array[string]): bool {
Set.of(xs).size() < xs.length()
}
println(hasDuplicates(["ada", "linus", "grace"]))
println(hasDuplicates(["ada", "linus", "ada"]))false
trueMeasure how much two sets of tags overlap, as the share of all tags they have in common:
let a = Set.of(["go", "rust", "ant"])
let b = Set.of(["ant", "zig", "go", "c"])
let shared = a.intersect(b).size()
let all = a.union(b).size()
println("${shared} of ${all} tags shared")2 of 5 tags sharedProperties and relations
Set.size agrees with len, Set.isEmpty, and the length of Set.values:
let cases: Array[Set[int]] = [Set.of([]), Set.of([1]), Set.of([1, 2, 2])]
for s in cases {
assert(s.size() == len(s))
assert(s.isEmpty() == (s.size() == 0))
assert(s.size() == s.values().length())
}The sizes of a union, an intersection, and a difference add up:
let a = Set.of([1, 2, 3, 4])
let b = Set.of([3, 4, 5])
assert(a.union(b).size() == a.size() + b.size() - a.intersect(b).size())
assert(a.difference(b).size() + a.intersect(b).size() == a.size())
assert(a.intersect(b).size() <= a.size())Possible issues
size is a function, not a field. Without parentheses it is a compile error:
let s = Set.of([1])
println(s.size) // error: Set[int] is not a record, so it has no field sizeSet.of([]) takes its element type from the expected type. Without one, the empty array has no element type to infer. Annotate it:
println(Set.of([]).size()) // error: cannot infer the element type of an empty array; annotate itA nullable set must be narrowed or reached through ?. first:
let s: Set[int] | null = null
println(s.size())
// error: Set[int] | null has no field or function size on every member: narrow the value firstSet.size takes only sets. Use Map.size for a map, Array.length for an array, or len, which takes them all:
println(Set.size([1, 2])) // error: cannot use Array[int] where Set[T] is expectedA set is a reference value: assigning or passing one shares it. An add through one name changes the size seen through the other:
let a = Set.of([1])
let b = a
b.add(2) // true
a.size() // 2See also
Set.isEmpty · Set.of · Set.add · Set.remove · Set.values · len · Map.size · Array.length
