Skip to content

Uniq

const Uniq: object

Defined in: Data/Uniq.ts:368

difference: <A>(other) => (set) => ReadonlySet<A>

Computes the set difference (set \ other).

A

ReadonlySet<A>

(set) => ReadonlySet<A>

empty: <A>() => ReadonlySet<A>

Creates an empty unique set.

A

ReadonlySet<A>

singleton to create a set with one element.

const set = Uniq.empty<number>(); // Set(0) {}

filter: <A>(predicate) => (set) => ReadonlySet<A>

Filters elements of a set by a predicate.

A

(item) => boolean

(set) => ReadonlySet<A>

filterMap for filtering and mapping simultaneously.

filterMap: <A, B>(transform) => (set) => ReadonlySet<B>

Transforms elements with a function returning Maybe, keeping only Some values.

A

B

(item) => Maybe<B>

(set) => ReadonlySet<B>

  • filter for filtering with a boolean predicate.
  • map for transforming without filtering.

from: object

array: <A>(items) => ReadonlySet<A> = fromArray

Creates a unique set from an array of elements.

A

readonly A[]

ReadonlySet<A>

Uniq.from.array([1, 2, 2, 3]); // Set(3) { 1, 2, 3 }

has: <A>(item) => (set) => boolean

Returns true when the set contains the given element.

A

A

(set) => boolean

  • insert for inserting an element into a set.
  • remove for deleting an element from a set.

insert: <A>(item) => (set) => ReadonlySet<A>

Returns a new set with the element added.

A

A

(set) => ReadonlySet<A>

  • remove for deleting an element from a set.
  • toggle for toggling presence of an element.

intersection: <A>(other) => (set) => ReadonlySet<A>

Computes the intersection of two sets.

A

ReadonlySet<A>

(set) => ReadonlySet<A>

  • union for combining elements from both sets.
  • difference for subtracting elements.

is: object

empty: <A>(set) => boolean = isEmpty

Returns true when the set contains zero elements.

A

ReadonlySet<A>

boolean

isNonEmpty to check if a set has elements.

Uniq.is.empty(Uniq.empty()); // true
Uniq.is.empty(Uniq.singleton(1)); // false

nonEmpty: <A>(set) => set is NonEmptySet<A> = isNonEmpty

Type guard — checks if a set contains at least one element.

A

ReadonlySet<A>

set is NonEmptySet<A>

isEmpty to check if a set is empty.

Uniq.is.nonEmpty(Uniq.singleton(1)); // true
Uniq.is.nonEmpty(Uniq.empty());      // false

subsetOf: <A>(other) => (set) => boolean = isSubsetOf

Returns true if set is a subset of other.

A

ReadonlySet<A>

(set) => boolean

pipe(Uniq.from.array([1, 2]), Uniq.is.subsetOf(Uniq.from.array([1, 2, 3]))); // true

map: <A, B>(transform) => (set) => ReadonlySet<B>

Transforms each element of a set into a new set.

A

B

(item) => B

(set) => ReadonlySet<B>

filterMap for mapping and filtering in a single step.

NonEmpty: object = UniqNonEmptyConst

Utilities for working with guaranteed non-empty sets (NonEmptySet).

from: object

set: <A>(set) => Maybe<NonEmptySet<A>>

Refines a standard Set to a NonEmptySet, returning Some if size > 0, None otherwise.

A

ReadonlySet<A>

Maybe<NonEmptySet<A>>

Uniq.NonEmpty.from.set(new Set([1, 2])); // Some(NonEmptySet)
Uniq.NonEmpty.from.set(new Set());        // None

map: <A, B>(transform) => (set) => NonEmptySet<B>

Transforms each element of a non-empty set, preserving non-emptiness.

A

B

(item) => B

(set) => NonEmptySet<B>

pipe(Uniq.NonEmpty.singleton(5), Uniq.NonEmpty.map(n => n * 2)); // NonEmptySet { 10 }

reduce: <A>(reducer) => (set) => A

Reduces a non-empty set using the first element as the initial accumulator.

A

(accumulator, item) => A

(set) => A

pipe(Uniq.NonEmpty.singleton(5), Uniq.NonEmpty.reduce((a, b) => a + b)); // 5

singleton: <A>(item) => NonEmptySet<A>

Creates a NonEmptySet containing a single element.

A

A

NonEmptySet<A>

Uniq.NonEmpty.singleton(42); // NonEmptySet(1) { 42 }

to: object

array: <A>(set) => NonEmptyArr<A>

Converts a NonEmptySet to a NonEmptyArr.

A

NonEmptySet<A>

NonEmptyArr<A>

Uniq.NonEmpty.to.array(Uniq.NonEmpty.singleton(42)); // [42]

reduce: <A, B>(initial, reducer) => (set) => B

Reduces elements of a set from an initial value.

A

B

B

(accumulator, item) => B

(set) => B

pipe(Uniq.from.array([1, 2, 3]), Uniq.reduce(0, (acc, n) => acc + n)); // 6

remove: <A>(item) => (set) => ReadonlySet<A>

Returns a new set with the element removed.

A

A

(set) => ReadonlySet<A>

insert for adding an element to a set.

singleton: <A>(item) => ReadonlySet<A>

Creates a unique set with a single element.

A

A

ReadonlySet<A>

empty to create an empty set.

Uniq.singleton(42); // Set(1) { 42 }

size: <A>(set) => number

Returns the number of elements in the set.

A

ReadonlySet<A>

number

Uniq.size(Uniq.from.array([1, 2, 3])); // 3

to: object

Converters from unique collections to other data structures.

array: <A>(set) => readonly A[] = toArray

Converts a unique set to a readonly array.

A

ReadonlySet<A>

readonly A[]

Uniq.to.array(Uniq.from.array([1, 2, 3])); // [1, 2, 3]

toggle: <A>(item) => (set) => ReadonlySet<A>

Toggles presence of an element in a set.

A

A

(set) => ReadonlySet<A>

  • insert for unconditionally adding an element.
  • remove for unconditionally removing an element.

union: <A>(other) => (set) => ReadonlySet<A>

Computes the union of two sets.

A

ReadonlySet<A>

(set) => ReadonlySet<A>