API / Belt / MutableSetInt

MutableSetInt

This module is Belt.MutableSet specialized with key type to be a int 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

RES
type value = int

The type of the set elements

t

RES
type t

Type of the sets.

make

RES
let make: unit => t

Returns empty set.

RES
let set = Belt.MutableSet.Int.make()

fromArray

RES
let fromArray: array<value> => t

Creates new set from array of elements.

RES
let s0 = Belt.MutableSet.Int.fromArray([1, 3, 2, 4]) s0->Belt.MutableSet.Int.toArray /* [1, 2, 3, 4] */

fromSortedArrayUnsafe

RES
let fromSortedArrayUnsafe: array<value> => t

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

copy

RES
let copy: t => t

Returns copy of a set.

RES
let s0 = Belt.MutableSet.Int.fromArray([1, 3, 2, 4]) let copied = s0->Belt.MutableSet.Int.copy copied->Belt.MutableSet.Int.toArray /* [1, 2, 3, 4] */

isEmpty

RES
let isEmpty: t => bool

Checks if set is empty.

RES
let empty = Belt.MutableSet.Int.fromArray([]) let notEmpty = Belt.MutableSet.Int.fromArray([1]) Belt.MutableSet.Int.isEmpty(empty) /* true */ Belt.MutableSet.Int.isEmpty(notEmpty) /* false */

has

RES
let has: (t, value) => bool

Checks if element exists in set.

RES
let set = Belt.MutableSet.Int.fromArray([1, 4, 2, 5]) set->Belt.MutableSet.Int.has(3) /* false */ set->Belt.MutableSet.Int.has(1) /* true */

add

RES
let add: (t, value) => unit

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

RES
let s0 = Belt.MutableSet.Int.make() s0->Belt.MutableSet.Int.add(1) s0->Belt.MutableSet.Int.add(2) s0->Belt.MutableSet.Int.add(2) s0->Belt.MutableSet.Int.toArray /* [1, 2] */

addCheck

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

mergeMany

RES
let mergeMany: (t, array<value>) => unit

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

RES
let set = Belt.MutableSet.Int.make() set->Belt.MutableSet.Int.mergeMany([5, 4, 3, 2, 1]) set->Belt.MutableSet.Int.toArray /* [1, 2, 3, 4, 5] */

remove

RES
let remove: (t, value) => unit

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

RES
let s0 = Belt.MutableSet.Int.fromArray([2, 3, 1, 4, 5]) s0->Belt.MutableSet.Int.remove(1) s0->Belt.MutableSet.Int.remove(3) s0->Belt.MutableSet.Int.remove(3) s0->Belt.MutableSet.Int.toArray /* [2,4,5] */

removeCheck

RES
let removeCheck: (t, value) => bool

removeMany

RES
let removeMany: (t, array<value>) => unit

Removes each element of array from set.

RES
let set = Belt.MutableSet.Int.fromArray([1, 2, 3, 4]) set->Belt.MutableSet.Int.removeMany([5, 4, 3, 2, 1]) set->Belt.MutableSet.Int.toArray /* [] */

union

RES
let union: (t, t) => t

Returns union of two sets.

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3, 5, 6]) let s1 = Belt.MutableSet.Int.fromArray([5, 2, 3, 1, 5, 4]) let union = Belt.MutableSet.Int.union(s0, s1) union->Belt.MutableSet.Int.toArray /* [1,2,3,4,5,6] */

intersect

RES
let intersect: (t, t) => t

Returns intersection of two sets.

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3, 5, 6]) let s1 = Belt.MutableSet.Int.fromArray([5, 2, 3, 1, 5, 4]) let intersect = Belt.MutableSet.Int.intersect(s0, s1) intersect->Belt.MutableSet.Int.toArray /* [2,3,5] */

diff

RES
let diff: (t, t) => t

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

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3, 5, 6]) let s1 = Belt.MutableSet.Int.fromArray([5, 2, 3, 1, 5, 4]) Belt.MutableSet.Int.toArray(Belt.MutableSet.Int.diff(s0, s1)) /* [6] */ Belt.MutableSet.Int.toArray(Belt.MutableSet.Int.diff(s1, s0)) /* [1,4] */

subset

RES
let subset: (t, t) => bool

Checks if second set is subset of first set.

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3, 5, 6]) let s1 = Belt.MutableSet.Int.fromArray([5, 2, 3, 1, 5, 4]) let s2 = Belt.MutableSet.Int.intersect(s0, s1) Belt.MutableSet.Int.subset(s2, s0) /* true */ Belt.MutableSet.Int.subset(s2, s1) /* true */ Belt.MutableSet.Int.subset(s1, s0) /* false */

cmp

RES
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.

eq

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

Checks if two sets are equal.

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3]) let s1 = Belt.MutableSet.Int.fromArray([3, 2, 5]) Belt.MutableSet.Int.eq(s0, s1) /* true */

forEachU

RES
let forEachU: (t, (. value) => unit) => unit

Same as forEach but takes uncurried functon.

forEach

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

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

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3, 5, 6]) let acc = ref(list{}) s0->Belt.MutableSet.Int.forEach(x => acc := Belt.List.add(acc.contents, x)) acc /* [6,5,3,2] */

reduceU

RES
let reduceU: (t, 'a, (. 'a, value) => 'a) => 'a

reduce

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.

RES
let s0 = Belt.MutableSet.Int.fromArray([5, 2, 3, 5, 6]) s0->Belt.MutableSet.Int.reduce(list{}, (acc, element) => acc->Belt.List.add(element) ) /* [6,5,3,2] */

everyU

RES
let everyU: (t, (. value) => bool) => bool

every

RES
let every: (t, value => bool) => bool

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

RES
let isEven = x => mod(x, 2) == 0 let s0 = Belt.MutableSet.Int.fromArray([2, 4, 6, 8]) s0->Belt.MutableSet.Int.every(isEven) /* true */

someU

RES
let someU: (t, (. value) => bool) => bool

some

RES
let some: (t, value => bool) => bool

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

RES
let isOdd = x => mod(x, 2) != 0 let s0 = Belt.MutableSet.Int.fromArray([1, 2, 4, 6, 8]) s0->Belt.MutableSet.Int.some(isOdd) /* true */

keepU

RES
let keepU: (t, (. value) => bool) => t

keep

RES
let keep: (t, value => bool) => t

Returns the set of all elements that satisfy the predicate.

RES
let isEven = x => mod(x, 2) == 0 let s0 = Belt.MutableSet.Int.fromArray([1, 2, 3, 4, 5]) let s1 = s0->Belt.MutableSet.Int.keep(isEven) s1->Belt.MutableSet.Int.toArray /* [2, 4] */

partitionU

RES
let partitionU: (t, (. value) => bool) => (t, t)

partition

RES
let partition: (t, value => bool) => (t, t)
RES
let isOdd = x => mod(x, 2) != 0 let s0 = Belt.MutableSet.Int.fromArray([1, 2, 3, 4, 5]) let (s1, s2) = s0->Belt.MutableSet.Int.partition(isOdd) s1->Belt.MutableSet.Int.toArray /* [1,3,5] */ s2->Belt.MutableSet.Int.toArray /* [2,4] */

size

RES
let size: t => int

Returns size of the set.

RES
let s0 = Belt.MutableSet.Int.fromArray([1, 2, 3, 4]) s0->Belt.MutableSet.Int.size /* 4 */

toList

RES
let toList: t => list<value>

Returns list of ordered set elements.

RES
let s0 = Belt.MutableSet.Int.fromArray([3, 2, 1, 5]) s0->Belt.MutableSet.Int.toList /* [1,2,3,5] */

toArray

RES
let toArray: t => array<value>

Returns array of ordered set elements.

RES
let s0 = Belt.MutableSet.Int.fromArray([3, 2, 1, 5]) s0->Belt.MutableSet.Int.toArray /* [1,2,3,5] */

minimum

RES
let minimum: t => option<value>

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

RES
let s0 = Belt.MutableSet.Int.make() let s1 = Belt.MutableSet.Int.fromArray([3, 2, 1, 5]) s0->Belt.MutableSet.Int.minimum /* None */ s1->Belt.MutableSet.Int.minimum /* Some(1) */

minUndefined

RES
let minUndefined: t => Js.undefined<value>

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

RES
let s0 = Belt.MutableSet.Int.make() let s1 = Belt.MutableSet.Int.fromArray([3, 2, 1, 5]) s0->Belt.MutableSet.Int.minUndefined /* undefined */ s1->Belt.MutableSet.Int.minUndefined /* 1 */

maximum

RES
let maximum: t => option<value>

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

RES
let s0 = Belt.MutableSet.Int.make() let s1 = Belt.MutableSet.Int.fromArray([3, 2, 1, 5]) s0->Belt.MutableSet.Int.maximum /* None */ s1->Belt.MutableSet.Int.maximum /* Some(5) */

maxUndefined

RES
let maxUndefined: t => Js.undefined<value>

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

RES
let s0 = Belt.MutableSet.Int.make() let s1 = Belt.MutableSet.Int.fromArray([3, 2, 1, 5]) s0->Belt.MutableSet.Int.maxUndefined /* undefined */ s1->Belt.MutableSet.Int.maxUndefined /* 5 */

get

RES
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.

RES
let s0 = Belt.MutableSet.Int.fromArray([1, 2, 3, 4, 5]) s0->Belt.MutableSet.Int.get(3) /* Some(3) */ s0->Belt.MutableSet.Int.get(20) /* None */

getUndefined

RES
let getUndefined: (t, value) => Js.undefined<value>

Same as get but returns undefined when element does not exist.

getExn

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

Same as get but raise when element does not exist.

split

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

Returns a tuple ((smaller, larger), present), present is true when element exist in set.

RES
let s0 = Belt.MutableSet.Int.fromArray([1, 2, 3, 4, 5]) let ((smaller, larger), present) = s0->Belt.MutableSet.Int.split(3) present /* true */ smaller->Belt.MutableSet.Int.toArray /* [1,2] */ larger->Belt.MutableSet.Int.toArray /* [4,5] */

checkInvariantInternal

RES
let checkInvariantInternal: t => unit

raise when invariant is not held