Set
Built-in type · Written [“red”, “blue”] where a Set type is expected
A Set holds values once each, and answers one question quickly: is this value in it? Use one
for the words already guessed, the squares already visited, or the names already seen. Its
type is written with the type of its values, as Set[String]. On this page, T stands for
that type.
var guessed: Set[String] = []guessed.add("e")guessed.add("e") # already there, so nothing changesprint(guessed.contains?("e")) # prints trueprint(guessed.count) # prints 1A set is written in square brackets, like a list. The brackets make a set only where a Set
type is expected, as in const primes: Set[Int] = [2, 3, 5, 7]; anywhere else they make a
list. to_set() turns a list into a set. A set prints in braces, as
{"red", "blue"}, and an empty one as {}.
A set keeps its values in the order they were first added, for for loops and printing. Two
sets are equal when they hold the same values, in any order. A set has no positions, so
colors[0] is an error; ask with contains? instead.
A value in a set must be one that never changes: a number, Bool, String, an enum value,
or a tuple or struct made only of those.
Like a list, a set is a value: assigning it to another variable gives that variable its own
copy. Only a var set can change.
At a glance
Section titled “At a glance”| Size and membership | |
|---|---|
count, empty?() |
How many values it has |
contains?(value) |
Whether a value is in it |
| Changing it | |
|---|---|
add(value), remove(value) |
Add or remove a value |
| Combining sets | |
|---|---|
union(other), intersection(other) |
Values in either, or in both |
difference(other), symmetric_difference(other) |
Values in only one |
subset?(other), superset?(other), disjoint?(other) |
How two sets relate |
| Visiting and asking | |
|---|---|
filter { ... }, reject { ... } |
The values a block accepts, or rejects, as a set |
each { ... }, each_with_index { ... }, reverse_each { ... } |
Run a block for each value |
map { ... }, flat_map { ... }, filter_map { ... } |
A list made from the values |
find { ... }, find_index { ... } |
The first value a block accepts |
any? { ... }, all? { ... }, none? { ... }, one? { ... }, count_where { ... } |
Questions about the values |
Operators
Section titled “Operators”| Operator | Meaning | Example |
|---|---|---|
== != |
Compare | two sets of the same values are equal, in any order |
Size and membership
Section titled “Size and membership”How many values the set has.
[3, 1, 3].to_set().count # → 2Whether the set has no values.
[3].to_set().empty?() # → falseWhether value is in the set. However large the set, this takes about the same time.
["red", "blue"].to_set().contains?("red") # → trueChanging it
Section titled “Changing it”These change the set, so they need a var set.
Adds value. When it is already there, nothing happens.
var seen: Set[String] = ["red"]seen.add("blue")seen.add("red")print(seen) # prints {"red", "blue"}Removes value. When it is not there, nothing happens.
var seen: Set[String] = ["red", "blue"]seen.remove("red")print(seen) # prints {"blue"}Combining sets
Section titled “Combining sets”These leave both sets as they were and give back a new one.
Every value in either set.
[2, 3, 5, 7].to_set().union([1, 3, 5, 7, 9].to_set()) # → {2, 3, 5, 7, 1, 9}intersection(other: Set[T]): Set[T]
The values in both sets.
[2, 3, 5, 7].to_set().intersection([1, 3, 5, 7, 9].to_set()) # → {3, 5, 7}difference(other: Set[T]): Set[T]
The values in this set that are not in other.
[2, 3, 5, 7].to_set().difference([1, 3, 5, 7, 9].to_set()) # → {2}symmetric_difference(other: Set[T]): Set[T]
The values in exactly one of the two sets.
[2, 3, 5, 7].to_set().symmetric_difference([1, 3, 5, 7, 9].to_set()) # → {2, 1, 9}Whether every value in this set is also in other.
[3, 5].to_set().subset?([2, 3, 5, 7].to_set()) # → truesuperset?(other: Set[T]): Bool
Whether this set has every value in other.
[2, 3, 5, 7].to_set().superset?([3, 5].to_set()) # → truedisjoint?(other: Set[T]): Bool
Whether the two sets have no values in common.
[2, 3, 5, 7].to_set().disjoint?([4, 6].to_set()) # → trueVisiting and asking
Section titled “Visiting and asking”These work as they do for a List. filter and reject give back a set; the
others that make something new give back a list.
filter { value: T => Bool }: Set[T]
The values the block accepts.
[2, 3, 5, 7].to_set().filter { n => n > 3 } # → {5, 7}reject { value: T => Bool }: Set[T]
The values the block does not accept.
Runs the block for each value, in order. It is the same as a for loop.
each_with_index { value: T, index: Int => ... }
Runs the block for each value, with its position in order.
reverse_each { value: T => ... }
Runs the block for each value, from last to first.
map { value: T => U }: List[U]
A list of what the block gives for each value.
[2, 3, 5, 7].to_set().map { n => n * 10 } # → [20, 30, 50, 70]flat_map { value: T => List[U] }: List[U]
The items of every list the block gives, joined into one list.
filter_map { value: T => U? }: List[U]
What the block gives for each value, leaving out every nothing.
The first value the block accepts, or nothing.
[2, 3, 5, 7].to_set().find { n => n > 3 } # → 5find_index { value: T => Bool }: Int?
The position of the first value the block accepts, or nothing.
any? { value: T => Bool }: Bool
Whether the block accepts at least one value.
[2, 3, 5, 7].to_set().any? { n => n > 5 } # → trueall? { value: T => Bool }: Bool
Whether the block accepts every value.
none? { value: T => Bool }: Bool
Whether the block accepts no value.
one? { value: T => Bool }: Bool
Whether the block accepts exactly one value.
count_where { value: T => Bool }: Int
How many values the block accepts.