You are currently looking at the v6.0 - v8.2 docs (Reason v3.6 syntax edition). You can find the latest API docs here.
(These docs cover all versions between v3 to v8 and are equivalent to the old BuckleScript docs before the rebrand)
MutableSetString
This module is Belt.MutableSet specialized with key type to be a string
type.
It is more efficient in general, the API is the same with Belt.MutableSet except its key type is fixed, and identity is not needed (using the built-in one)
value
REtype value = string;
The type of the set elements.
t
REtype t;
The type of sets.
make
RElet make: unit => t;
Returns empty set.
RElet set = Belt.MutableSet.String.make();
fromArray
RElet fromArray: array(value) => t;
Creates new set from array of elements.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "orange", "banana"|])
s0->Belt.MutableSet.String.toArray; /* [|"apple", "banana", "orange"|] */
fromSortedArrayUnsafe
RElet fromSortedArrayUnsafe: array(value) => t;
The same as [fromArray][#fromarray] except it is after assuming the input array is already sorted.
copy
RElet copy: t => t;
Returns copy of a set.
RElet s0 = Belt.MutableSet.String.fromArray([|"orange", "apple"|])
let copied = s0->Belt.MutableSet.String.copy;
copied->Belt.MutableSet.String.toArray /* [|"apple", "orange"|] */
isEmpty
RElet isEmpty: t => bool;
Checks if set is empty.
RElet empty = Belt.MutableSet.String.fromArray([||]);
let notEmpty = Belt.MutableSet.String.fromArray([|"apple"|]);
Belt.MutableSet.String.isEmpty(empty); /* true */
Belt.MutableSet.String.isEmpty(notEmpty); /* false */
has
RElet has: (t, value) => bool;
Checks if element exists in set.
RElet set = Belt.MutableSet.String.fromArray([|"apple", "orange", "banana"|]);
set->Belt.MutableSet.String.has("strawberry") /* false */
set->Belt.MutableSet.String.has("apple") /* true */
add
RElet add: (t, value) => unit;
Adds element to set. If element existed in set, value is unchanged.
RElet s0 = Belt.MutableSet.String.make();
s0->Belt.MutableSet.String.add("apple");
s0->Belt.MutableSet.String.add("banana");
s0->Belt.MutableSet.String.add("banana");
s0->Belt.MutableSet.String.toArray; /* [|"apple", "banana"|] */
addCheck
RElet addCheck: (t, value) => bool;
mergeMany
RElet mergeMany: (t, array(value)) => unit;
Adds each element of array to set.
RElet set = Belt.MutableSet.String.make();
set->Belt.MutableSet.String.mergeMany([|"apple", "banana", "orange", "strawberry"|]);
set->Belt.MutableSet.String.toArray; /* [|"apple", "banana", "orange", "strawberry"|] */
remove
RElet remove: (t, value) => unit;
Removes element from set. If element wasn't existed in set, value is unchanged.
RElet s0 = Belt.MutableSet.String.fromArray([|"orange", "banana", "apple"|]);
s0->Belt.MutableSet.String.remove("apple");
s0->Belt.MutableSet.String.remove("banana");
s0->Belt.MutableSet.String.remove("banana");
s0->Belt.MutableSet.String.toArray; /* [|"orange"|] */
removeCheck
RElet removeCheck: (t, value) => bool;
removeMany
RElet removeMany: (t, array(value)) => unit;
Removes each element of array from set.
RElet set = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange"|]);
set->Belt.MutableSet.String.removeMany([|"strawberry", "apple", "banana", "orange"|]);
set->Belt.MutableSet.String.toArray; /* [||] */
union
RElet union: (t, t) => t;
Returns union of two sets.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange", "carrot"|]);
let s1 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange", "strawberry"|]);
let union = Belt.MutableSet.String.union(s0, s1);
union->Belt.MutableSet.String.toArray; /* [|"apple", "banana", "carrot", "orange", "strawberry"|] */
intersect
RElet intersect: (t, t) => t;
Returns intersection of two sets.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange", "carrot"|]);
let s1 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange", "strawberry"|]);
let intersect = Belt.MutableSet.String.intersect(s0, s1);
intersect->Belt.MutableSet.String.toArray; /* [|"apple", "banana", "orange"|] */
diff
RElet diff: (t, t) => t;
Returns elements from first set, not existing in second set.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange", "carrot"|]);
let s1 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange", "strawberry"|]);
Belt.MutableSet.String.toArray(Belt.MutableSet.String.diff(s0, s1)); /* [|"carrot"|] */
Belt.MutableSet.String.toArray(Belt.MutableSet.String.diff(s1, s0)); /* [|"strawberry"|] */
subset
RElet subset: (t, t) => bool;
Checks if second set is subset of first set.
RElet s0 = Belt.MutableSet.String.fromArray([|"5", "2", "3", "5", "6"|]);
let s1 = Belt.MutableSet.String.fromArray([|"5", "2", "3", "1", "5", "4"|]);
let s2 = Belt.MutableSet.String.intersect(s0, s1);
Belt.MutableSet.String.subset(s2, s0); /* true */
Belt.MutableSet.String.subset(s2, s1); /* true */
Belt.MutableSet.String.subset(s1, s0); /* false */
cmp
RElet 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.
eq
RElet eq: (t, t) => bool;
Checks if two sets are equal.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "orange"|]);
let s1 = Belt.MutableSet.String.fromArray([|"orange", "apple"|]);
Belt.MutableSet.String.eq(s0, s1); /* true */
forEachU
RElet forEachU: (t, [@bs] (value => unit)) => unit;
Same as forEach but takes uncurried functon.
forEach
RElet forEach: (t, value => unit) => unit;
Applies function f
in turn to all elements of set in increasing order.
RElet s0 = Belt.MutableSet.String.fromArray([|"banana", "orange", "apple"|]);
let acc = ref([]);
s0->Belt.MutableSet.String.forEach(x => {
acc := Belt.List.add(acc^, x)
});
acc; /* ["orange", "banana", "apple"] */
reduceU
RElet reduceU: (t, 'a, [@bs] (('a, value) => 'a)) => 'a;
reduce
RElet 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.
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.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "orange"|]);
s0->Belt.MutableSet.String.reduce(0, (acc, element) =>
acc + String.length(element)
); /* 11 */
everyU
RElet everyU: (t, [@bs] (value => bool)) => bool;
every
RElet every: (t, value => bool) => bool;
Checks if all elements of the set satisfy the predicate. Order unspecified.
RElet hasAtLeastFiveChars = x => String.length(x) >= 5;
let s0 = Belt.MutableSet.String.fromArray([|"apple", "carrot"|]);
s0->Belt.MutableSet.String.every(hasAtLeastFiveChars); /* true */
someU
RElet someU: (t, [@bs] (value => bool)) => bool;
some
RElet some: (t, value => bool) => bool;
Checks if at least one element of the set satisfies the predicate.
RElet hasFiveChars = x => String.length(x) == 5;
let s0 = Belt.MutableSet.String.fromArray([|"strawberry", "apple"|]);
s0->Belt.MutableSet.String.some(hasFiveChars); /* true */
keepU
RElet keepU: (t, [@bs] (value => bool)) => t;
keep
RElet keep: (t, value => bool) => t;
Returns the set of all elements that satisfy the predicate.
RElet hasFiveChars = x => String.length(x) == 5;
let s0 = Belt.MutableSet.String.fromArray([|"apple", "orange", "banana"|]);
let s1 = s0->Belt.MutableSet.String.keep(hasFiveChars);
s1->Belt.MutableSet.String.toArray; /* [|"apple"|] */
partitionU
RElet partitionU: (t, [@bs] (value => bool)) => (t, t);
partition
RElet 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.
RElet hasFiveChars = x => String.length(x) == 5;
let s0 = Belt.MutableSet.String.fromArray([|"apple", "carrot"|]);
let (s1, s2) = s0->Belt.MutableSet.String.partition(hasFiveChars);
s1->Belt.MutableSet.String.toArray; /* [|"apple"|] */
s2->Belt.MutableSet.String.toArray; /* [|"carrot"|] */
size
RElet size: t => int;
Returns size of the set.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple"|]);
s0->Belt.MutableSet.String.size; /* 1 */
toList
RElet toList: t => list(value);
Returns list of ordered set elements.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "watermelon"|]);
s0->Belt.MutableSet.String.toList; /* ["apple", "watermelon"] */
toArray
RElet toArray: t => array(value);
Returns array of ordered set elements.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "watermelon"|]);
s0->Belt.MutableSet.String.toArray; /* [|"apple", "watermelon"|] */
minimum
RElet minimum: t => option(value);
Returns minimum value of the collection. None
if collection is empty.
RElet s0 = Belt.MutableSet.String.make();
let s1 = Belt.MutableSet.String.fromArray([|"apple", "orange"|]);
s0->Belt.MutableSet.String.minimum; /* None */
s1->Belt.MutableSet.String.minimum; /* Some("apple") */
minUndefined
RElet minUndefined: t => Js.undefined(value);
Returns minimum value of the collection. undefined
if collection is empty.
RElet s0 = Belt.MutableSet.String.make();
let s1 = Belt.MutableSet.String.fromArray([|"apple", "orange"|]);
s0->Belt.MutableSet.String.minUndefined; /* undefined */
s1->Belt.MutableSet.String.minUndefined; /* "apple" */
maximum
RElet maximum: t => option(value);
Returns maximum value of the collection. None
if collection is empty.
RElet s0 = Belt.MutableSet.String.make();
let s1 = Belt.MutableSet.String.fromArray([|"apple", "orange"|]);
s0->Belt.MutableSet.String.maximum; /* None */
s1->Belt.MutableSet.String.maximum; /* Some("orange") */
maxUndefined
RElet maxUndefined: t => Js.undefined(value);
Returns maximum value of the collection. undefined
if collection is empty.
RElet s0 = Belt.MutableSet.String.make();
let s1 = Belt.MutableSet.String.fromArray([|"apple", "orange"|]);
s0->Belt.MutableSet.String.maxUndefined; /* undefined */
s1->Belt.MutableSet.String.maxUndefined; /* orange */
get
RElet 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.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "carrot"|]);
s0->Belt.MutableSet.String.get("carrot"); /* Some("carrot") */
s0->Belt.MutableSet.String.get("watermelon"); /* None */
getUndefined
RElet getUndefined: (t, value) => Js.undefined(value);
Same as get but returns undefined
when element does not exist.
getExn
RElet getExn: (t, value) => value;
Same as get but raise when element does not exist.
split
RElet split: (t, value) => ((t, t), bool);
Returns a tuple ((smaller, larger), present)
, present
is true when element exist in set.
RElet s0 = Belt.MutableSet.String.fromArray([|"apple", "banana", "orange"|]);
let ((smaller, larger), present) = s0->Belt.MutableSet.String.split("banana");
present; /* true */
smaller->Belt.MutableSet.String.toArray; /* [|"apple"|] */
larger->Belt.MutableSet.String.toArray; /* [|"orange"|] */
checkInvariantInternal
RElet checkInvariantInternal: t => unit;
raise when invariant is not held