set
import set
This module provides functionalities for working with mathematical sets.
Sets are collections of unique values. You can iterate through sets in
the same order in which they were initialized or set by
set.Set.add(). Two Sets containing exactly the same
elements, irrespective of order, are equal, whether compared with ==
or with set.Set.equals().
The example below shows a brief introduction to working with sets.
import set
var my_set = set()
my_set.add(11) # <Set(1) {11}>
my_set.add(56) # <Set(2) {11, 56}>
my_set.add(97) # <Set(3) {11, 56, 97}>
my_set.add('some text') # <Set(4) {11, 56, 97, some text}>
var o = { a: 1, b: 2 }
my_set.add(o)
# o is referencing a different object, but contains the same value
# so it will not be added
my_set.add({ a: 1, b: 2 })
my_set.contains(11) # true
my_set.contains(32) # false, since 32 has not been added to the set
my_set.contains(97) # true
my_set.contains(121 ** 0.5) # true
my_set.contains('Some Text'.lower()) # true
my_set.contains(o) # true
Sets objects can also be created with initial values (or their actual values) by passing a list or a dictionary to the constructor. If a list is passed, all unique elements in the list will be added to the Set object.
For example,
import set
var set_a = set([1, 2, 3, 4, 5])
var set_b = set([4, 5, 6, 7, 8])
echo set_a.intersect(set_b) # <Set(2) {4, 5}>
When a dictionary is passed to the Set constructor, the dictionary keys which are unique themselves will be added to the Set object.
For example:
import set
var set_a = set({
a: 10,
b: 21,
})
echo set_a # <Set(2) {a, b}>
The module is callable. set(...) is the same call as set.set(...),
documented below.
The set API
Every public name in set, wherever it is declared. Each links to the
page that documents it.
| Name | Kind | Summary |
|---|---|---|
set.Set | class | The Set class provides some methods that allow you to compose sets like you would with mathematical… |
set.set | function | Default export function for the set.Set class. |
Functions
set()
set.set(items: ?list|dict) -> set.Set
Default export function for the set.Set class.
Parameters
items(?list|dict)
Returns set.Set
Classes
Set
class set.Set
The Set class provides some methods that allow you to compose sets like you would with mathematical operations.
- printable — has a
@to_string(), soechoandprint()show something useful - serializable — has a
@to_json(), so it can be handed straight tojson.encode() - valueable — has a
@to_value(), so it converts to a plain Zuri value - numeric — converts to a number
Constructor
set.Set(items: ?list|dict)
Creates a new Set object from a list or dictionary or an empty Set object when no argument is passed.
Parameters
items(?list|dict)
Set.union()
set.Set.union(other: instance) -> set.Set
Returns a new set containing elements which are in either or both of this set and the given set.
Parameters
other(set.Set)
Returns set.Set
Set.intersect()
set.Set.intersect(other: instance) -> set.Set
Returns a new set containing elements in both this set and the given set.
Parameters
other(set.Set)
Returns set.Set
Set.difference()
set.Set.difference(other: instance) -> set.Set
Returns a new set containing elements in this set but not in the given set.
Parameters
other(set.Set)
Returns set.Set
Set.symmetric_difference()
set.Set.symmetric_difference(other: instance) -> set.Set
Returns a new set containing elements which are in either this set or the given set, but not in both.
Parameters
other(set.Set)
Returns set.Set
Set.is_disjoint()
set.Set.is_disjoint(other: instance) -> bool
Returns a boolean indicating if this set has no elements in common with the given set.
Parameters
other(set.Set)
Returns bool
Set.is_subset()
set.Set.is_subset(other: instance) -> bool
Returns a boolean indicating if all elements of this set are in the given set.
Parameters
other(set.Set)
Returns bool
Set.is_superset()
set.Set.is_superset(other: instance) -> bool
Returns a boolean indicating if all elements of the given set are in this set.
Parameters
other(set.Set)
Returns bool
Set.is_empty()
set.Set.is_empty() -> bool
Returns a boolean value indicating whether this set is an empty set or not.
Returns bool
Set.contains()
set.Set.contains(value) -> bool
Returns a boolean asserting whether an element is present with the given value in the Set or not.
Parameters
value(any)
Returns bool
Set.length()
set.Set.length() -> number
Returns the number of values in the Set object.
Returns number
Set.clear()
set.Set.clear() -> bool
Removes all elements from the Set object.
Returns bool
Set.remove()
set.Set.remove(value) -> bool
Removes the element associated to the value and returns a boolean
asserting whether an element was successfully removed or not. Once an
element is removed, calling set.contains(value) will return false
afterwards.
Parameters
value(any)
Returns bool
Set.pop()
set.Set.pop() -> any
Removes and returns an arbitrary element from the Set.
Returns any
Raises Error if the Set is empty.
Set.add()
set.Set.add(value) -> bool
Inserts a new element with the specified value in to the Set object, if there isn’t an element with the same value already in the Set.
Parameters
value(any)
Returns bool
Set.clone()
set.Set.clone() -> set.Set
Returns a new Set which is an exact replica of the current Set.
Returns set.Set
Set.equals()
set.Set.equals(other: instance) -> bool
Returns a boolean indicating whether this Set and other contain
exactly the same elements, irrespective of order. == makes the same
comparison.
Parameters
other(set.Set)
Returns bool
Set.update()
set.Set.update(other: instance) -> set.Set
Adds every element of other to this Set in place, and returns this Set
(so calls can chain). Unlike union(), this mutates self instead of
returning a new Set.
Parameters
other(set.Set)
Returns set.Set
Set.intersection_update()
set.Set.intersection_update(other: instance) -> set.Set
Keeps only the elements this Set has in common with other, in place,
and returns this Set. Unlike intersect(), this mutates self instead
of returning a new Set.
Parameters
other(set.Set)
Returns set.Set
Set.difference_update()
set.Set.difference_update(other: instance) -> set.Set
Removes every element other also contains from this Set, in place, and
returns this Set. Unlike difference(), this mutates self instead of
returning a new Set.
Parameters
other(set.Set)
Returns set.Set
Set.symmetric_difference_update()
set.Set.symmetric_difference_update(other: instance) -> set.Set
Keeps only the elements in exactly one of this Set or other, in place,
and returns this Set. Unlike symmetric_difference(), this mutates
self instead of returning a new Set.
Parameters
other(set.Set)
Returns set.Set
Set.each()
set.Set.each(callback: function)
Calls function callback once for each value present in the Set, in
insertion order.
Parameters
callback(function)
Set.to_string()
set.Set.to_string() -> string
Returns a string that represents the current Set.
Returns string
Set.to_list()
set.Set.to_list() -> list
Returns the current Set as a list of elements.
Returns list
Richard Ore, 2025