Uniq — Set Utilities
2.00 kB minified + gzipped
Two services compute the regions eligible for a deployment. Each starts from a requested list, removes the regions under maintenance, and publishes the result. A requested list that repeats a region survives the subtraction with the duplicate intact, so the two services disagree about how many regions remain and neither reports an error.
Core Behavior
Section titled “Core Behavior”Uniq wraps ReadonlySet and provides data-last operations for membership, transformation, and set
algebra. Membership follows JavaScript equality; object values are compared by reference.
from.arrayremoves duplicates and keeps the first occurrence order;to.arrayreads values in insertion order.union,intersection, anddifferencereturn new sets without changing their inputs;differencekeeps the input values absent from the supplied set.insertandremovereturn the input set unchanged when membership would not change;togglealways returns a new set.mapcollapses values that transform to the same result.
Reach for Uniq when values are unique by definition and the question is membership.
Arr keeps order and duplicates and offers Arr.uniq for one-off deduplication.
The choosing guide covers the other collection modules.
Example: Deployment Regions
Section titled “Example: Deployment Regions”An operator requests deployment regions, and a maintenance window disables a subset. The eligible regions are the requested regions that are not disabled, and the same sets answer whether every disabled region is a recognized one.
difference tests each requested value against the disabled set and keeps the values it does not
find:
flowchart TD
Requested["Requested regions"] --> Disabled{"Value in disabled set?"}
Disabled -->|Yes| Removed["Omitted from eligible"]
Disabled -->|No| Kept["Kept in eligible"]
Selecting Regions
Section titled “Selecting Regions”from.array removed the second east, and difference removed west. The subset check reads as
disabled is a subset of known, which is the direction is.subsetOf takes.
Handoff: A Non-Empty Result After Set Algebra
Section titled “Handoff: A Non-Empty Result After Set Algebra”Set algebra can empty the set. When a later step cannot accept an empty collection, refine the result once:
The Maybe records the only remaining failure, and Uniq.NonEmpty.to.array returns an array whose
type still says it has an element. Non-Empty Collections lists the guards for
every collection module.
Core Operations
Section titled “Core Operations”Creating and Reading
Section titled “Creating and Reading”| Signature | What it does | Result |
|---|---|---|
Uniq.empty() / Uniq.singleton(item) / Uniq.from.array(items) |
Creates a set. | ReadonlySet |
Uniq.has(item) / Uniq.size |
Tests membership / reads the size. | Boolean / number |
Uniq.is.empty / Uniq.is.nonEmpty / Uniq.is.subsetOf(other) |
Inspects a set. | Boolean; is.nonEmpty narrows |
Uniq.to.array(set) |
Copies values in insertion order. | readonly A[] |
Updating and Combining
Section titled “Updating and Combining”| Signature | What it does | Result |
|---|---|---|
Uniq.insert(item) / Uniq.remove(item) / Uniq.toggle(item) |
Adds, removes, or toggles a value. | A set, or the input when membership did not change |
Uniq.map(transform) / Uniq.filter(predicate) / Uniq.filterMap(transform) |
Transforms or selects values. | A new set; transformed duplicates collapse |
Uniq.union(other) / Uniq.intersection(other) / Uniq.difference(other) |
Combines or compares membership. | A new set; difference removes values in other |
Uniq.reduce(initial, reducer) |
Accumulates values. | The final accumulator |
Working with Non-Empty Sets
Section titled “Working with Non-Empty Sets”| Signature | What it does | Result |
|---|---|---|
Uniq.NonEmpty.from.set(set) |
Checks for at least one value. | Maybe<Uniq.NonEmpty<A>> |
Uniq.NonEmpty.singleton(item) / Uniq.NonEmpty.map(transform) |
Creates a one-value set / transforms it. | Uniq.NonEmpty<A> |
Uniq.NonEmpty.reduce(reducer) |
Folds from the first value. | The final value |
Uniq.NonEmpty.to.array(set) |
Copies values. | NonEmptyArr<A> |
Failure Behavior and Edge Cases
Section titled “Failure Behavior and Edge Cases”Set membership follows same-value equality. Object values are compared by reference, so two objects with the same fields are different values:
insert returns the input set when the value is already present, and remove does the same when it
is absent, so the result can be the same reference. toggle always copies. map keeps the
transformed value’s first position and drops later duplicates.
intersection and difference keep the input set’s iteration order; union lists the input values
first and appends values that exist only in the supplied set. The empty set is a subset of every
set, so is.subsetOf returns true when the input has no values.
Uniq.NonEmpty.from.set checks the size at the moment of the call and returns the same set, not a
copy; mutating the underlying set through another reference invalidates the guarantee. For
value-based uniqueness, transform each object through a stable key first;
Arr.uniqBy applies the same projection without building a set.