Uniq — unique collection utilities
A Set is the right structure when you care about membership rather than order — when the
question is “is this value in the collection?” rather than “what is at index 5?”. The native
Set API does the job, but it’s built around mutation. Adding or removing an item changes the
set in place, which makes it awkward to use in a pipeline where the previous state still matters.
Uniq wraps ReadonlySet<A> with pure, data-last functions that compose with pipe. Every
operation that “changes” the collection returns a new one. The original is never touched.
Creating a collection
Section titled “Creating a collection”The most common constructor is Uniq.fromArray, which accepts an array and automatically
discards any duplicates:
fromArray is safe to call on any array — if there are no duplicates, you get back a set
representing exactly those values.
Checking membership
Section titled “Checking membership”Uniq.has is the primary membership test. It returns true if the item is in the collection
and false if not:
Uniq.isSubsetOf tests whether every item in one collection also appears in another:
Adding and removing items
Section titled “Adding and removing items”Uniq.insert returns a new collection with the item added. If the item is already present,
the original collection is returned unchanged — no copy is made. Uniq.remove works the same
way in reverse:
Transforming and filtering
Section titled “Transforming and filtering”Uniq.map applies a function to every item and returns a new collection of the results.
If the function produces duplicates, they are merged automatically:
Uniq.filter keeps only the items for which the predicate returns true:
Set operations
Section titled “Set operations”The three classic set operations — union, intersection, and difference — work as you
would expect from a maths or database context:
These compose naturally when the answer to a question is itself a set operation:
Folding and converting
Section titled “Folding and converting”Uniq.reduce collapses the collection into a single value. Uniq.toArray converts it back
to an array when you need to pass the values to something that doesn’t speak Set:
Composing it all
Section titled “Composing it all”Uniq operations chain in pipe like any other utility:
When to use Uniq
Section titled “When to use Uniq”Use Uniq when:
- You need to track a collection of values where duplicates don’t make sense — user IDs, feature flags, active sessions, selected tags
- You need fast membership checks (
has) without scanning an array - You’re combining, comparing, or subtracting groups of values with
union,intersection, ordifference - You’re deduplicating an array and want the result to stay as a set
Keep using plain arrays when:
- Order matters and you need index-based access
- Duplicates are meaningful (e.g. a list of events where the same event can occur twice)
- You need
Arroperations likezip,flatMap, orscan