sets

Larky module containing common hash-set algorithms.

An empty set can be created using: sets.make(), or it can be created with some starting values if you pass it an sequence: sets.make([1, 2, 3]). This returns a larky.struct containing all of the values as keys in a dictionary - this means that all passed in values must be hashable. The values in the set can be retrieved using sets.to_list(my_set).

An arbitrary object can be tested whether it is a set generated by sets.make() or not with the types.is_set() method in types.star.

sets.ImmutableSet(iterable=None)

Immutable set class.

sets.Set(iterable=None)

Mutable set class.

sets.contains(a, e)

Checks for the existence of an element in a set.

Parameters:

  • a – a set, as returned by sets.make().

  • e – the element to look for.

Returns: True if the element exists in the set, False if the element does not.

sets.copy(s)

Creates a new set from another set.

Parameters:

s – a set, as returned by sets.make().

Returns: a new set containing the same elements as s.

sets.difference(a, b)

Returns: the elements in a that are not in b.

Parameters:

  • a – a set, as returned by sets.make().

  • b – a set, as returned by sets.make().

Returns: a set containing the elements that are in a but not in b.

sets.disjoint(a, b)

Returns: whether two sets are disjoint.

Two sets are disjoint if they have no elements in common.

Parameters:

  • a – a set, as returned by sets.make().

  • b – a set, as returned by sets.make().

Returns: true if a and b are disjoint, False otherwise.

sets.insert(s, e)

Inserts an element into the set.

Element must be hashable. This mutates the original set.

Parameters:

  • s – a set, as returned by sets.make().

  • e – the element to be inserted.

Returns: the set s with e included.

sets.intersection(a, b)

Returns: the intersection of two sets.

Parameters:

  • a – a set, as returned by sets.make().

  • b – A set, as returned by sets.make().

Returns: a set containing the elements that are in both a and b.

sets.is_equal(a, b)

Returns: whether two sets are equal.

Parameters:

  • a – a set, as returned by sets.make().

  • b – a set, as returned by sets.make().

Returns: True if a is equal to b, False otherwise.

sets.is_subset(a, b)

Returns: whether a is a subset of b.

Parameters:

  • a – a set, as returned by sets.make().

  • b – a set, as returned by sets.make().

Returns: True if a is a subset of b, False otherwise.

sets.length(s)

Returns: the number of elements in a set.

Parameters:

s – a set, as returned by sets.make().

Returns: an integer representing the number of elements in the set.

sets.make(elements=None)

Creates a new set. All elements must be hashable.

Parameters:

elements – sequence to construct the set out of.

Returns: a set containing the passed in values.

sets.remove(s, e)

Removes an element from the set.

Element must be hashable. This mutates the original set.

Parameters:

  • s – a set, as returned by sets.make().

  • e – the element to be removed.

Returns: The set s with e removed.

sets.repr(s)

Returns a string value representing the set.

Parameters:

s – a set, as returned by sets.make().

Returns: astring representing the set.

sets.to_list(s)

Creates a list from the values in the set.

Parameters:

s – a set, as returned by sets.make().

Returns: a list of values inserted into the set.

sets.union(*args)

Returns the union of several sets.

Parameters:

*args – an arbitrary number of sets.

Returns: the set union of all sets in *args.

Last updated