Pyro

A dynamically-typed, garbage-collected scripting language.

Version 0.17.2

Sets



A set object, set, is an unordered collection of distinct values.

$set() -> set
$set(arg: iterable) -> set

Creates a new set. If arg is iterable, initializes the new set by iterating over its values.

Alternatively, you can create a set by draining an iterator, e.g.

var set = "supercalifragilistic":runes():to_set();
assert set:count() == 12;

Set Literals

You can create a set using literal syntax, e.g.

var set = {1, 2, 3};

Trailing commas are allowed, e.g.

var set = {
    1,
    2,
    3,
};

Note that the empty literal {} will create an empty map — use $set() to create an empty set.

Iterating

Sets are iterable, e.g.

for item in {1, 2, 3} {
    echo item;
}

Containment

You can check if a set contains an item using the in operator, e.g.

var set = {"foo", "bar", "baz"};

if "foo" in set {
    echo "found";
}

This is equivalent to calling the set's :contains() method.

Set Operators

Comparison Operators

Two sets A and B will compare as equal using the == operator, A == B, if they are set-equivalent, i.e. if they contain the same items in any order.

Methods

:add(item: any)

Adds an item to the set. This is a null operation if the set already contains a member equal to item.

:clear()

Removes all items from the set.

:contains(item: any) -> bool

Returns true if the set contains a member equal to item, otherwise false.

:count() -> i64

Returns the number of items in the set.

:difference(other: set) -> set

Returns a new set containing the difference of the two sets — i.e. the set of all items that are in receiver but not in other.

Equivalent to receiver - other.

:intersection(other: set) -> set

Returns a new set containing the intersection of the two sets — i.e. the set of all items that are in both receiver and other.

Equivalent to receiver & other.

:is_empty() -> bool

Returns true if the set is empty.

:is_equal_to(other: set) -> bool

Returns true if receiver is set-equivalent to other, i.e. if the two sets contain the same items in any order.

Equivalent to receiver == other.

:is_proper_subset_of(other: set) -> bool

Returns true if receiver is a proper subset of other.

Equivalent to receiver < other.

:is_proper_superset_of(other: set) -> bool

Returns true if receiver is a proper superset of other.

Equivalent to receiver > other.

:is_subset_of(other: set) -> bool

Returns true if receiver is a subset of other.

Equivalent to receiver <= other.

:is_superset_of(other: set) -> bool

Returns true if receiver is a superset of other.

Equivalent to receiver >= other.

:remove(item: any)

Removes item from the set. This is a null operation if the set does not contain a member equal to item.

:symmetric_difference(other: set) -> set

Returns a new set containing the symmetric difference of the two sets — i.e. the set of all items that are either in receiver or in other but not both.

Equivalent to receiver ^ other.

:union(other: set) -> set

Returns a new set containing the union of the two sets — i.e. the set of all items that are in either receiver or other.

Equivalent to receiver | other.

:values() -> iter

Returns an iterator over the set's values.