Set
Set holds the functions of the built-in set type Set[T], a hash set keyed by == that keeps insertion order. There is no literal: the empty set is Set.of([]).
Details
- Records, arrays, maps, and sets are members by contents.
- A set keeps insertion order. Adding a member that is already there keeps its position; a member removed and added again goes to the end.
- Set.add and Set.remove change the set in place and report whether they changed it. Every other function returns a new value and leaves its arguments untouched.
- A set is a reference: copying a variable shares the set. Two sets are
==when they have the same members, in any order. - A member is hashed when it goes in. Changing an array or a record after it became a member is a program error: the set may no longer find it.
Basic examples
Set.of builds a set from an array and drops the repeats. Set.has asks for a member:
let tags = Set.of(["red", "blue", "red"])
tags // Set.of(["red", "blue"])
tags.has("blue") // true
tags.has("green") // false
len(tags) // 2Set.add and Set.remove change the set and return whether they did:
let seen = Set.of([1, 2])
seen.add(3) // true
seen.add(3) // false
seen.remove(1) // true
seen // Set.of([2, 3])A for loop visits the members in insertion order:
let langs = Set.of(["go", "ant", "go", "zig"])
for lang in langs {
println(lang)
}go
ant
zigScope
There is no set literal. The empty set is Set.of([]), with its element type from an annotation or from where it goes:
let empty: Set[string] = Set.of([])
empty // Set.of([])
empty.isEmpty() // trueRecords and arrays are members by contents:
type Point = { x: int, y: int }
let visited = Set.of([Point{ x: 0, y: 0 }])
visited.has(Point{ x: 0, y: 0 }) // true
Set.of([[1, 2], [1, 2], [2, 1]]) // Set.of([[1, 2], [2, 1]])A set can hold null when its element type includes it:
let answers: Set[bool | null] = Set.of([true, null, null])
answers // Set.of([true, null])
answers.has(null) // trueCalls chain through ., or the set goes down a pipe:
let a = Set.of([1, 2, 3, 4])
let b = Set.of([3, 4, 5])
a.intersect(b).values() // [3, 4]
a | Set.difference(b) | Set.size() // 2Functions by job
Building a set
- Set.of: a new set holding the first of each value of an array, in order
Size and membership
- Set.size: the number of members; the same as
len(s) - Set.isEmpty: whether there are none
- Set.has: whether a value is a member
Changing a set in place
- Set.add: add a member;
truewhen it was not already there - Set.remove: remove a member;
truewhen it was there
Members as an array
- Set.values: the members in insertion order
Combining sets
- Set.union: the members of
a, then those ofbthatalacks - Set.intersect: the members of
athatbhas - Set.difference: the members of
athatblacks - Set.isSubset: whether every member of
ais a member ofb
Identity
- Set.same: whether two sets are the same set, not only equal
Applications
Print each line the first time it appears. Set.add answers the question and records the line in one call:
let seen: Set[string] = Set.of([])
for line in ["b", "a", "b", "c", "a"] {
if seen.add(line) {
println(line)
}
}b
a
cFind the required settings a map lacks:
let required = Set.of(["host", "port", "user"])
let given = Map{ host: "localhost", user: "ada" }
required.difference(Set.of(given.keys())).values() // ["port"]Check that a word uses only allowed letters:
let allowed = Set.of("abcdef".chars())
Set.of("faced".chars()).isSubset(allowed) // true
Set.of("faded!".chars()).isSubset(allowed) // falseCopy a set, so that changing the copy leaves the original alone:
let a = Set.of([1, 2])
let c = Set.of(a.values())
c.add(3) // true
a // Set.of([1, 2])List the members in sorted order:
let tags = Set.of(["red", "blue", "green"])
tags.values().sort() // ["blue", "green", "red"]Properties and relations
len, Set.size, and Set.values agree:
let s = Set.of(["x", "y", "x"])
assert(len(s) == s.size())
assert(len(s.values()) == s.size())
assert(s.isEmpty() == (s.size() == 0))The members of Set.of(xs), in order, are what Array.unique returns:
let xs = [4, 2, 4, 9, 2]
assert(Set.of(xs).values() == xs.unique())== ignores order, while Set.values shows it:
let a = Set.of([1, 2, 3])
let b = Set.of([3, 2, 1])
assert(a == b)
assert(a.values() != b.values())A union holds both sets, and an intersection lies inside each:
let a = Set.of([1, 2, 3])
let b = Set.of([2, 3, 4])
assert(a.isSubset(a.union(b)) && b.isSubset(a.union(b)))
assert(a.intersect(b).isSubset(a) && a.intersect(b).isSubset(b))
assert(a.difference(b).intersect(b).isEmpty())Possible issues
Set.of([]) with nothing to give it a type is a compile error:
let seen = Set.of([]) // error: cannot infer the element type of an empty array; annotate itA set has no indexing. Take the members as an array first:
let s = Set.of(["a", "b"])
s[0] // error: Set[string] cannot be indexedlet s = Set.of(["a", "b"])
s.values()[0] // "a"Copying a set variable copies the reference. A change through one name shows through the other:
let a = Set.of([1])
let b = a
b.add(2) // true
a // Set.of([1, 2])
Set.same(a, b) // trueSee also
- Array, Map: the other built-in containers
- Array.unique: the distinct elements of an array, as an array
- Map.keys: the keys of a map, ready for
Set.of
