SetInt
Specalized when value type is int
, more efficient than the generic type, its compare behavior is fixed using the built-in comparison.
value
REStype value = int
The type of the set elements.
t
REStype t
Type of the sets.
empty
RESlet empty: t
Empty set
RESlet s0 = Belt.Set.Int.empty
fromArray
RESlet fromArray: array<value> => t
Creates new set from array of elements.
RESlet s0 = Belt.Set.Int.fromArray([1, 3, 2, 4])
s0->Belt.Set.Int.toArray /* [1, 2, 3, 4] */
fromSortedArrayUnsafe
RESlet fromSortedArrayUnsafe: array<value> => t
The same as [fromArray][#fromarray] except it is after assuming the input array is already sorted.
isEmpty
RESlet isEmpty: t => bool
Checks if set is empty.
RESlet empty = Belt.Set.Int.fromArray([])
let notEmpty = Belt.Set.Int.fromArray([1])
Belt.Set.Int.isEmpty(empty) /* true */
Belt.Set.Int.isEmpty(notEmpty) /* false */
has
RESlet has: (t, value) => bool
Checks if element exists in set.
RESlet set = Belt.Set.Int.fromArray([1, 4, 2, 5])
set->Belt.Set.Int.has(3) /* false */
set->Belt.Set.Int.has(1) /* true */
add
RESlet add: (t, value) => t
Adds element to set. If element existed in set, value is unchanged.
RESlet s0 = Belt.Set.Int.empty
let s1 = s0->Belt.Set.Int.add(1)
let s2 = s1->Belt.Set.Int.add(2)
let s3 = s2->Belt.Set.Int.add(2)
s0->Belt.Set.Int.toArray /* [] */
s1->Belt.Set.Int.toArray /* [1] */
s2->Belt.Set.Int.toArray /* [1, 2] */
s3->Belt.Set.Int.toArray /* [1,2 ] */
s2 == s3 /* true */
mergeMany
RESlet 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
RESlet set = Belt.Set.Int.empty
let newSet = set->Belt.Set.Int.mergeMany([5, 4, 3, 2, 1])
newSet->Belt.Set.Int.toArray /* [1, 2, 3, 4, 5] */
remove
RESlet remove: (t, value) => t
Removes element from set. If element wasn't existed in set, value is unchanged.
RESlet s0 = Belt.Set.Int.fromArray([2, 3, 1, 4, 5])
let s1 = s0->Belt.Set.Int.remove(1)
let s2 = s1->Belt.Set.Int.remove(3)
let s3 = s2->Belt.Set.Int.remove(3)
s1->Belt.Set.Int.toArray /* [2,3,4,5] */
s2->Belt.Set.Int.toArray /* [2,4,5] */
s2 == s3 /* true */
removeMany
RESlet 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.
RESlet set = Belt.Set.Int.fromArray([1, 2, 3, 4])
let newSet = set->Belt.Set.Int.removeMany([5, 4, 3, 2, 1])
newSet->Belt.Set.Int.toArray /* [] */
union
RESlet union: (t, t) => t
Returns union of two sets.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3, 5, 6])
let s1 = Belt.Set.Int.fromArray([5, 2, 3, 1, 5, 4])
let union = Belt.Set.Int.union(s0, s1)
union->Belt.Set.Int.toArray /* [1,2,3,4,5,6] */
intersect
RESlet intersect: (t, t) => t
Returns intersection of two sets.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3, 5, 6])
let s1 = Belt.Set.Int.fromArray([5, 2, 3, 1, 5, 4])
let intersect = Belt.Set.Int.intersect(s0, s1)
intersect->Belt.Set.Int.toArray /* [2,3,5] */
diff
RESlet diff: (t, t) => t
Returns elements from first set, not existing in second set.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3, 5, 6])
let s1 = Belt.Set.Int.fromArray([5, 2, 3, 1, 5, 4])
Belt.Set.Int.toArray(Belt.Set.Int.diff(s0, s1)) /* [6] */
Belt.Set.Int.toArray(Belt.Set.Int.diff(s1, s0)) /* [1,4] */
subset
RESlet subset: (t, t) => bool
Checks if second set is subset of first set.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3, 5, 6])
let s1 = Belt.Set.Int.fromArray([5, 2, 3, 1, 5, 4])
let s2 = Belt.Set.Int.intersect(s0, s1)
Belt.Set.Int.subset(s2, s0) /* true */
Belt.Set.Int.subset(s2, s1) /* true */
Belt.Set.Int.subset(s1, s0) /* false */
cmp
RESlet 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
RESlet eq: (t, t) => bool
Checks if two sets are equal.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3])
let s1 = Belt.Set.Int.fromArray([3, 2, 5])
Belt.Set.Int.eq(s0, s1) /* true */
forEachU
RESlet forEachU: (t, (. value) => unit) => unit
Same as forEach but takes uncurried functon.
forEach
RESlet forEach: (t, value => unit) => unit
Applies function f
in turn to all elements of set in increasing order.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3, 5, 6])
let acc = ref(list{})
s0->Belt.Set.Int.forEach(x => acc := Belt.List.add(acc.contents, x))
acc /* [6,5,3,2] */
reduceU
RESlet reduceU: (t, 'a, (. 'a, value) => 'a) => 'a
reduce
RESlet 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.
RESlet s0 = Belt.Set.Int.fromArray([5, 2, 3, 5, 6])
s0->Belt.Set.Int.reduce(list{}, (acc, element) => acc->Belt.List.add(element)) /* [6,5,3,2] */
everyU
RESlet everyU: (t, (. value) => bool) => bool
every
RESlet every: (t, value => bool) => bool
Checks if all elements of the set satisfy the predicate. Order unspecified.
RESlet isEven = x => mod(x, 2) == 0
let s0 = Belt.Set.Int.fromArray([2, 4, 6, 8])
s0->Belt.Set.Int.every(isEven) /* true */
someU
RESlet someU: (t, (. value) => bool) => bool
some
RESlet some: (t, value => bool) => bool
Checks if at least one element of the set satisfies the predicate.
RESlet isOdd = x => mod(x, 2) != 0
let s0 = Belt.Set.Int.fromArray([1, 2, 4, 6, 8])
s0->Belt.Set.Int.some(isOdd) /* true */
keepU
RESlet keepU: (t, (. value) => bool) => t
keep
RESlet keep: (t, value => bool) => t
Returns the set of all elements that satisfy the predicate.
RESlet isEven = x => mod(x, 2) == 0
let s0 = Belt.Set.Int.fromArray([1, 2, 3, 4, 5])
let s1 = s0->Belt.Set.Int.keep(isEven)
s1->Belt.Set.Int.toArray /* [2,4] */
partitionU
RESlet partitionU: (t, (. value) => bool) => (t, t)
partition
RESlet 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.
RESlet isOdd = x => mod(x, 2) != 0
let s0 = Belt.Set.Int.fromArray([1, 2, 3, 4, 5])
let (s1, s2) = s0->Belt.Set.Int.partition(isOdd)
s1->Belt.Set.Int.toArray /* [1,3,5] */
s2->Belt.Set.Int.toArray /* [2,4] */
size
RESlet size: t => int
Returns size of the set.
RESlet s0 = Belt.Set.Int.fromArray([1, 2, 3, 4])
s0->Belt.Set.Int.size /* 4 */
toList
RESlet toList: t => list<value>
Returns list of ordered set elements.
RESlet s0 = Belt.Set.Int.fromArray([3, 2, 1, 5])
s0->Belt.Set.Int.toList /* [1,2,3,5] */
toArray
RESlet toArray: t => array<value>
Returns array of ordered set elements.
RESlet s0 = Belt.Set.Int.fromArray([3, 2, 1, 5])
s0->Belt.Set.Int.toArray /* [1,2,3,5] */
minimum
RESlet minimum: t => option<value>
Returns minimum value of the collection. None
if collection is empty.
RESlet s0 = Belt.Set.Int.empty
let s1 = Belt.Set.Int.fromArray([3, 2, 1, 5])
s0->Belt.Set.Int.minimum /* None */
s1->Belt.Set.Int.minimum /* Some(1) */
minUndefined
RESlet minUndefined: t => Js.undefined<value>
Returns minimum value of the collection. undefined
if collection is empty.
RESlet s0 = Belt.Set.Int.empty
let s1 = Belt.Set.Int.fromArray([3, 2, 1, 5])
s0->Belt.Set.Int.minUndefined /* undefined */
s1->Belt.Set.Int.minUndefined /* 1 */
maximum
RESlet maximum: t => option<value>
Returns maximum value of the collection. None
if collection is empty.
RESlet s0 = Belt.Set.Int.empty
let s1 = Belt.Set.Int.fromArray([3, 2, 1, 5])
s0->Belt.Set.Int.maximum /* None */
s1->Belt.Set.Int.maximum /* Some(5) */
maxUndefined
RESlet maxUndefined: t => Js.undefined<value>
Returns maximum value of the collection. undefined
if collection is empty.
RESlet s0 = Belt.Set.Int.empty
let s1 = Belt.Set.Int.fromArray([3, 2, 1, 5])
s0->Belt.Set.Int.maxUndefined /* undefined */
s1->Belt.Set.Int.maxUndefined /* 5 */
get
RESlet 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.
RESlet s0 = Belt.Set.Int.fromArray([1, 2, 3, 4, 5])
s0->Belt.Set.Int.get(3) /* Some(3) */
s0->Belt.Set.Int.get(20) /* None */
getUndefined
RESlet getUndefined: (t, value) => Js.undefined<value>
Same as get but returns undefined
when element does not exist.
getExn
RESlet getExn: (t, value) => value
Same as get but raise when element does not exist.
split
RESlet split: (t, value) => ((t, t), bool)
Returns a tuple ((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.
RESlet s0 = Belt.Set.Int.fromArray([1, 2, 3, 4, 5])
let ((smaller, larger), present) = s0->Belt.Set.Int.split(3)
present /* true */
smaller->Belt.Set.Int.toArray /* [1,2] */
larger->Belt.Set.Int.toArray /* [4,5] */
checkInvariantInternal
RESlet checkInvariantInternal: t => unit
raise when invariant is not held