Sets

Claro sets are much like Python sets, with a fixed, single type for all elements. You may initialize them with many elements and then check for membership in the set later.

Fig 1:


var mySet: {int} = {1, 6, -12};
print(10 in mySet);
print(6 in mySet);

Output:

false
true

Adding Elements to a Mutable Set

Elements can be added to a set by making use of the sets::add function from the stdlib's sets module.

Fig 2:


# Adds the specified element to this set if it is not already present. If this set already contains the element, the
# call leaves the set unchanged and returns false. This ensures that sets never contain duplicate elements.
#
# Returns: true if this set did not already contain the specified element.
function add<T>(s: mut {T}, t: T) -> boolean;

Fig 3:


var s = mut {1, 2};
print(10 in s);
_ = sets::add(s, 10);  # <-- Explicitly ignoring output of function call.
print(10 in s);

Output:

false
true