Standardbibliothek
Antra::Set
import Antra::SetSet operations for Antra.
Antra::Set combines immutable set operations with mutable set mutations.
A tagged set (%Seen<1, 2>) is a set with a label: every function takes
it, and the ones that return a set keep the tag of the (left) set.
Mutable Operations:
Functions like add, remove work in-place on mutable set values and return
the mutated set.
Immutable Operations:
Functions like union, intersect, diff treat sets as immutable values
and return new sets.
Set Membership and Conversion:
Functions like has, size, and from_array provide standard set queries
and conversions. For materializing sets as arrays, use Set.to_array(...).
Überblick#
Antra::Set ist die API für Mengenoperationen auf immutable Sets. Das Modul
bietet funktionale Set-Operationen wie Union, Schnitt und Differenz.
import Antra::Set as Set
Set.has(set, value)
Set.add(set, value)
Set.remove(set, value)
Set.size(set)
Set.union(a, b)
Set.intersect(a, b)
Set.diff(a, b)
Set.from_array(array)
Set.to_array(set)
Set.empty()Tagged Sets wie %MyTag<1, 2, 3> behalten ihr Tag bei Operationen,
wenn das linke Argument das Tag trägt.
Referenz#
import Antra::Set as S
# Grundlegende Operationen
S.empty()
S.has(s, value)
S.add(s, value)
S.remove(s, value)
S.size(s)
# Mengenoperationen
S.union(a, b)
S.intersect(a, b)
S.diff(a, b)
# Konvertierung
S.from_array(array)
S.to_array(s)
# Tagged Sets
my ids = %Ids<1, 2, 3>
my ids2 = S.add(ids, 4) # behält Tag: %Ids<1, 2, 3, 4>Alle Set-Funktionen arbeiten funktional (nicht-mutierend). Sie geben neue Set-Werte zurück, ohne das übergebene Set zu verändern.
Spread-Syntax für Sets:
my a = %<1, 2>
my b = %<2, 3>
my union = %<..a, ..b> # %<1, 2, 3>
my diff = %<..a, -..b> # %<1>Funktionen#
addallanycountdiffeachemptyempty?filterfindfind_mapflat_mapfold_leftfrom_arrayhasintersectmapmember?reducerejectremovesizeto_arrayunion
fn add(mut set :- Set[T], elem :- T) :- Set[T]Adds elem to set and returns the mutated set. If set is tagged,
the tag is preserved.
my seen = %<1>
Set.add(seen, 2) # => %<1, 2>fn all(set :- Set[T], pred :- fn(T, ?Int) -> Bool) :- BoolWhether pred holds for every element; true for an empty set.
Set.all(%<2, 4>, \_ % 2 == 0) # => truefn any(set :- Set[T], pred :- fn(T, ?Int) -> Bool) :- BoolWhether pred holds for some element.
Set.any(%<1, 5>, \_ > 4) # => truefn count(set :- Set[T]) :- Int
fn count(set :- Set[T], pred :- fn(T, ?Int) -> Bool) :- IntThe number of elements, or of those for which pred holds.
Set.count(%<1, 2, 3>) # => 3
Set.count(%<1, 2, 3>, \_ % 2 == 1) # => 2fn diff(a :- Set[T], b :- Set[T]) :- Set[T]Returns the difference of two sets (elements in a but not in b,
also a \\ b). If a is tagged, the tag is preserved.
Set.diff(%<1, 2, 3>, %<2>) # => %<1, 3>fn each(set :- Set[T], f :- fn(T, ?Int) -> U)Calls f on every element for its effect; returns the last result.
Set.each(%<"a", "b">, |x| -> println(x))fn empty() :- Set[T]Returns an empty set (the same as %<>).
Set.size(Set.empty()) # => 0fn empty?(set :- Set[T]) :- BoolWhether the set has no elements.
Set.empty?(%<>) # => truefn filter(set :- Set[T], pred :- fn(T, ?Int) -> Bool) :- [T]The elements for which pred holds, as an array.
Set.filter(%<1, 2, 3, 4>, \_ > 2) # => [3, 4]fn find(set :- Set[T], pred :- fn(T, ?Int) -> Bool) :- Option[T]The first element (in the set's order) for which pred holds.
Set.find(%<1, 5, 7>, \_ > 4) # => %Some[5]fn find_map(set :- Set[T], f :- fn(T, ?Int) -> Option[U]) :- Option[U]The first %Some[...] that f gives (f returns an Option).
Set.find_map(%<1, 4>, |x| -> if x > 2 then %Some[x * 10] else %None[]) # => %Some[40]fn flat_map(set :- Set[T], f :- fn(T, ?Int) -> [U]) :- [U]Maps every element to an array and joins them, one level.
Set.flat_map(%<1, 2>, |x| -> [x, x * 10]) # => [1, 10, 2, 20]fn fold_left(set :- Set[T], acc :- A, f :- fn(A, T, ?Int) -> A) :- AThe same as reduce, with the accumulator-first name.
Set.fold_left(%<"a", "b">, "", |acc, s| -> acc <> s) # => "ab"fn from_array(array :- [T]) :- Set[T]Creates a set from an array, removing duplicates.
Set.from_array([1, 2, 1]) # => %<1, 2>fn has(set :- Set[T], elem) :- BoolChecks if elem is a member of the set (also elem in set).
Set.has(%<1, 2>, 3) # => falsefn intersect(a :- Set[T], b :- Set[T]) :- Set[T]Returns the intersection of two sets.
Set.intersect(%<1, 2>, %<2, 3>) # => %<2>fn map(set :- Set[T], f :- fn(T, ?Int) -> U) :- [U]Applies f to every element; the results as an array (duplicates
stay).
Set.map(%<1, 2, 3>, \_ % 2) # => [1, 0, 1]fn member?(set :- Set[T], value) :- BoolWhether value is an element (the same as Set.has).
Set.member?(%<1, 2>, 2) # => truefn reduce(set :- Set[T], acc :- A, f :- fn(A, T, ?Int) -> A) :- AFolds the elements: f(acc, x) for every element, starting with
acc.
Set.reduce(%<1, 2, 3>, 0, |acc, x| -> acc + x) # => 6fn reject(set :- Set[T], pred :- fn(T, ?Int) -> Bool) :- [T]The elements for which pred does not hold, as an array.
Set.reject(%<1, 2, 3, 4>, \_ > 2) # => [1, 2]fn remove(mut set :- Set[T], elem) :- Set[T]Removes elem from set and returns the mutated set. If set is tagged,
the tag is preserved.
my seen = %<1, 2>
Set.remove(seen, 1) # => %<2>fn size(set :- Set[T]) :- IntReturns the number of elements in the set.
Set.size(%<"a", "b">) # => 2fn to_array(set :- Set[T]) :- [T]The elements as an array, in the set's order (sorted by their text).
Set.to_array(%<3, 1, 2>) # => [1, 2, 3]fn union(a :- Set[T], b :- Set[T]) :- Set[T]Returns the union of two sets. If the left set is tagged, the tag is preserved.
Set.union(%<1, 2>, %<2, 3>) # => %<1, 2, 3>