API / JavaScript / Belt / SetString

SetString

Specalized when value type is string, more efficient than the generic type, its compare behavior is fixed using the built-in comparison.

type value = string;

The type of the set elements.

type t;

The type of sets.

let empty: t;

Empty set

RE
let s0 = Belt.Set.String.empty;
let fromArray: array(value) => t;

Creates new set from array of elements.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "orange", "banana"|]) s0->Belt.Set.String.toArray; /* [|"apple", "banana", "orange"|] */
let fromSortedArrayUnsafe: array(value) => t;

The same as [fromArray][#fromarray] except it is after assuming the input array is already sorted.

let isEmpty: t => bool;

Checks if set is empty.

RE
let empty = Belt.Set.String.fromArray([||]); let notEmpty = Belt.Set.String.fromArray([|"apple"|]); Belt.Set.String.isEmpty(empty); /* true */ Belt.Set.String.isEmpty(notEmpty); /* false */
let has: (t, value) => bool;

Checks if element exists in set.

RE
let set = Belt.Set.String.fromArray([|"apple", "orange", "banana"|]); set->Belt.Set.String.has("strawberry") /* false */ set->Belt.Set.String.has("apple") /* true */
let add: (t, value) => t;

Adds element to set. If element existed in set, value is unchanged.

RE
let s0 = Belt.Set.String.empty; let s1 = s0->Belt.Set.String.add("apple"); let s2 = s1->Belt.Set.String.add("banana"); let s3 = s2->Belt.Set.String.add("banana"); s0->Belt.Set.String.toArray; /* [||] */ s1->Belt.Set.String.toArray; /* [|"apple"|] */ s2->Belt.Set.String.toArray; /* [|"apple", "banana"|] */ s3->Belt.Set.String.toArray; /* [|"apple", "banana"|] */ s2 == s3; /* true */
let mergeMany: (t, array(value)) => t;

Adds each element of array to set. Unlike add, the reference of return value might be changed even if all values in array already exist in set

RE
let set = Belt.Set.String.empty; let newSet = set->Belt.Set.String.mergeMany([|"apple", "banana", "orange", "strawberry"|]); newSet->Belt.Set.String.toArray; /* [|"apple", "banana", "orange", "strawberry"|] */
let remove: (t, value) => t;

Removes element from set. If element wasn't existed in set, value is unchanged.

RE
let s0 = Belt.Set.String.fromArray([|"orange", "banana", "apple"|]); let s1 = s0->Belt.Set.String.remove("apple"); let s2 = s1->Belt.Set.String.remove("banana"); let s3 = s2->Belt.Set.String.remove("banana"); s1->Belt.Set.String.toArray; /* [|"orange", "banana"|] */ s2->Belt.Set.String.toArray; /* [|"orange"|] */ s2 == s3; /* true */
let removeMany: (t, array(value)) => t;

Removes each element of array from set. Unlike remove, the reference of return value might be changed even if any values in array not existed in set.

RE
let set = Belt.Set.String.fromArray([|"apple", "banana", "orange"|]); let newSet = set->Belt.Set.String.removeMany([|"strawberry", "apple", "banana", "orange"|]); newSet->Belt.Set.String.toArray; /* [||] */
let union: (t, t) => t;

Returns union of two sets.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "banana", "orange", "carrot"|]); let s1 = Belt.Set.String.fromArray([|"apple", "banana", "orange", "strawberry"|]); let union = Belt.Set.String.union(s0, s1); union->Belt.Set.String.toArray; /* [|"apple", "banana", "carrot", "orange", "strawberry"|] */
let intersect: (t, t) => t;

Returns intersection of two sets.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "banana", "orange", "carrot"|]); let s1 = Belt.Set.String.fromArray([|"apple", "banana", "orange", "strawberry"|]); let intersect = Belt.Set.String.intersect(s0, s1); intersect->Belt.Set.String.toArray; /* [|"apple", "banana", "orange"|] */
let diff: (t, t) => t;

Returns elements from first set, not existing in second set.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "banana", "orange", "carrot"|]); let s1 = Belt.Set.String.fromArray([|"apple", "banana", "orange", "strawberry"|]); Belt.Set.String.toArray(Belt.Set.String.diff(s0, s1)); /* [|"carrot"|] */ Belt.Set.String.toArray(Belt.Set.String.diff(s1, s0)); /* [|"strawberry"|] */
let subset: (t, t) => bool;

Checks if second set is subset of first set.

RE
let s0 = Belt.Set.String.fromArray([|"5", "2", "3", "5", "6"|]); let s1 = Belt.Set.String.fromArray([|"5", "2", "3", "1", "5", "4"|]); let s2 = Belt.Set.String.intersect(s0, s1); Belt.Set.String.subset(s2, s0); /* true */ Belt.Set.String.subset(s2, s1); /* true */ Belt.Set.String.subset(s1, s0); /* false */
let cmp: (t, t) => int;

Total ordering between sets. Can be used as the ordering function for doing sets of sets. It compares size first and then iterates over each element following the order of elements.

let eq: (t, t) => bool;

Checks if two sets are equal.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "orange"|]); let s1 = Belt.Set.String.fromArray([|"orange", "apple"|]); Belt.Set.String.eq(s0, s1); /* true */
let forEachU: (t, [@bs] (value => unit)) => unit;

Same as forEach but takes uncurried functon.

let forEach: (t, value => unit) => unit;

Applies function f in turn to all elements of set in increasing order.

RE
let s0 = Belt.Set.String.fromArray([|"banana", "orange", "apple"|]); let acc = ref([]); s0->Belt.Set.String.forEach(x => { acc := Belt.List.add(acc^, x) }); acc; /* ["orange", "banana", "apple"] */
let reduceU: (t, 'a, [@bs] (('a, value) => 'a)) => 'a;
let reduce: (t, 'a, ('a, value) => 'a) => 'a;

Applies function f to each element of set in increasing order. Function f has two parameters: the item from the set and an “accumulator”, which starts with a value of initialValue. reduce returns the final value of the accumulator.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "orange"|]); s0->Belt.Set.String.reduce(0, (acc, element) => acc + String.length(element) ); /* 11 */
let everyU: (t, [@bs] (value => bool)) => bool;
let every: (t, value => bool) => bool;

Checks if all elements of the set satisfy the predicate. Order unspecified.

RE
let hasAtLeastFiveChars = x => String.length(x) >= 5; let s0 = Belt.Set.String.fromArray([|"apple", "carrot"|]); s0->Belt.Set.String.every(hasAtLeastFiveChars); /* true */
let someU: (t, [@bs] (value => bool)) => bool;
let some: (t, value => bool) => bool;

Checks if at least one element of the set satisfies the predicate.

RE
let hasFiveChars = x => String.length(x) == 5; let s0 = Belt.Set.String.fromArray([|"strawberry", "apple"|]); s0->Belt.Set.String.some(hasFiveChars); /* true */
let keepU: (t, [@bs] (value => bool)) => t;
let keep: (t, value => bool) => t;

Returns the set of all elements that satisfy the predicate.

RE
let hasFiveChars = x => String.length(x) == 5; let s0 = Belt.Set.String.fromArray([|"apple", "orange", "banana"|]); let s1 = s0->Belt.Set.String.keep(hasFiveChars); s1->Belt.Set.String.toArray; /* [|"apple"|] */
let partitionU: (t, [@bs] (value => bool)) => (t, t);
let partition: (t, value => bool) => (t, t);

Returns a pair of sets, where first is the set of all the elements of set that satisfy the predicate, and second is the set of all the elements of set that do not satisfy the predicate.

RE
let hasFiveChars = x => String.length(x) == 5; let s0 = Belt.Set.String.fromArray([|"apple", "carrot"|]); let (s1, s2) = s0->Belt.Set.String.partition(hasFiveChars); s1->Belt.Set.String.toArray; /* [|"apple"|] */ s2->Belt.Set.String.toArray; /* [|"carrot"|] */
let size: t => int;

Returns size of the set.

RE
let s0 = Belt.Set.String.fromArray([|"apple"|]); s0->Belt.Set.String.size; /* 1 */
let toList: t => list(value);

Returns list of ordered set elements.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "watermelon"|]); s0->Belt.Set.String.toList; /* ["apple", "watermelon"] */
let toArray: t => array(value);

Returns array of ordered set elements.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "watermelon"|]); s0->Belt.Set.String.toArray; /* [|"apple", "watermelon"|] */
let minimum: t => option(value);

Returns minimum value of the collection. None if collection is empty.

RE
let s0 = Belt.Set.String.empty; let s1 = Belt.Set.String.fromArray([|"apple", "orange"|]); s0->Belt.Set.String.minimum; /* None */ s1->Belt.Set.String.minimum; /* Some("apple") */
let minUndefined: t => Js.undefined(value);

Returns minimum value of the collection. undefined if collection is empty.

RE
let s0 = Belt.Set.String.empty; let s1 = Belt.Set.String.fromArray([|"apple", "orange"|]); s0->Belt.Set.String.minUndefined; /* undefined */ s1->Belt.Set.String.minUndefined; /* "apple" */
let maximum: t => option(value);

Returns maximum value of the collection. None if collection is empty.

RE
let s0 = Belt.Set.String.empty; let s1 = Belt.Set.String.fromArray([|"apple", "orange"|]); s0->Belt.Set.String.maximum; /* None */ s1->Belt.Set.String.maximum; /* Some("orange") */
let maxUndefined: t => Js.undefined(value);

Returns maximum value of the collection. undefined if collection is empty.

RE
let s0 = Belt.Set.String.empty; let s1 = Belt.Set.String.fromArray([|"apple", "orange"|]); s0->Belt.Set.String.maxUndefined; /* undefined */ s1->Belt.Set.String.maxUndefined; /* orange */
let get: (t, value) => option(value);

Returns the reference of the value which is equivalent to value using the comparator specifiecd by this collection. Returns None if element does not exist.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "carrot"|]); s0->Belt.Set.String.get("carrot"); /* Some("carrot") */ s0->Belt.Set.String.get("watermelon"); /* None */
let getUndefined: (t, value) => Js.undefined(value);

See get - returns undefined when element does not exist.

let getExn: (t, value) => value;

See get - raise when element does not exist.

let split: (t, value) => ((t, t), bool);

Returns a triple ((l, r), present), where l is the set of elements of set that are strictly less than value, r is the set of elements of set that are strictly greater than value, present is false if set contains no element equal to value, or true if set contains an element equal to value.

RE
let s0 = Belt.Set.String.fromArray([|"apple", "banana", "orange"|]); let ((smaller, larger), present) = s0->Belt.Set.String.split("banana"); present; /* true */ smaller->Belt.Set.String.toArray; /* [|"apple"|] */ larger->Belt.Set.String.toArray; /* [|"orange"|] */
let checkInvariantInternal: t => unit;

raise when invariant is not held