set/union
Union of two sets (stable insertion order)
Usage
C:=set/union(A, B)C:=A∪B
Description
Computes the union of two sets A and B, returning every distinct element that appears in either input. The implementation wraps a Rust IndexSet::union iterator, so the result preserves stable insertion order:
All elements from
Aappear first inA's order.Then any elements from
Bthat are not already inAare appended inB's order.
Input
Arguments:
A: set of T (or an array treated as a set; duplicates ignored)B: set of T (or an array treated as a set; duplicates ignored)
Element type constraints:
T must be hashable and comparable for equality (Eq and Hash in Rust).
Common element kinds include integers, strings, symbols, and booleans.
Output
Result:
- C : set of T containing all unique elements from A and B, ordered with A first
and then any new elements from B.
Examples
Simple union of two small sets
Duplicates in inputs are ignored
Order is stable by first appearance (IndexSet semantics)
Details
Set semantics:
Inputs are coerced to sets by dropping duplicates using hash and equality.
The result contains each distinct element exactly once.
Stable insertion order:
Matches Rust IndexSet behavior: iteration order reflects first insertion.
A determines the ordering of shared elements; new elements come from B in their original order.
Performance:
Average O(|A| + |B|) time with hashing; memory is O(|A| + |B|) for the union.
Determinism:
Given equal inputs, output order is deterministic across runs and platforms because it derives from insertion order rather than raw hash iteration order.
Errors
Incorrect number of arguments (expects exactly two).
Unsupported element type (not hashable or not comparable for equality).
Inconsistent element kinds between A and B.