Dict
This module separates identity from data. It is a bit more verbose but slightly more efficient due to the fact that there is no need to pack identity and data back after each operation.
t
type t<'value, 'identity>
'value
is the element type
'identity
the identity of the collection
cmp
type cmp<'value, 'id> = Belt_Id.cmp<'value, 'id>
Type of compare function.
empty
let empty: t<'value, 'id>
fromArray
let fromArray: (array<'value>, ~cmp: cmp<'value, 'id>) => t<'value, 'id>
Creates new set from array of elements.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([1, 3, 2, 4], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.toArray /* [1, 2, 3, 4] */
fromSortedArrayUnsafe
let fromSortedArrayUnsafe: array<'value> => t<'value, 'id>
The same as [fromArray][#fromarray] except it is after assuming the input array is already sorted.
isEmpty
let isEmpty: t<'a, 'b> => bool
Checks if set is empty.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let empty = Belt.Set.Dict.fromArray([], ~cmp=IntCmp.cmp)
let notEmpty = Belt.Set.Dict.fromArray([1], ~cmp=IntCmp.cmp)
Belt.Set.Dict.isEmpty(empty) /* true */
Belt.Set.Dict.isEmpty(notEmpty) /* false */
has
let has: (t<'value, 'id>, 'value, ~cmp: cmp<'value, 'id>) => bool
Checks if an element exists in the set.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let set = Belt.Set.Dict.fromArray([1, 4, 2, 5], ~cmp=IntCmp.cmp)
set->Belt.Set.Dict.has(3, ~cmp=IntCmp.cmp) /* false */
set->Belt.Set.Dict.has(1, ~cmp=IntCmp.cmp) /* true */
add
let add: (
t<'value, 'id>,
'value,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
Adds element to set. If element existed in set, value is unchanged.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.empty
let s1 = s0->Belt.Set.Dict.add(1, ~cmp=IntCmp.cmp)
let s2 = s1->Belt.Set.Dict.add(2, ~cmp=IntCmp.cmp)
let s3 = s2->Belt.Set.Dict.add(2, ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.toArray /* [] */
s1->Belt.Set.Dict.toArray /* [1] */
s2->Belt.Set.Dict.toArray /* [1, 2] */
s3->Belt.Set.Dict.toArray /* [1,2 ] */
s2 == s3 /* true */
mergeMany
let mergeMany: (
t<'value, 'id>,
array<'value>,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
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
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let set = Belt.Set.Dict.empty
let newSet = set->Belt.Set.Dict.mergeMany([5, 4, 3, 2, 1], ~cmp=IntCmp.cmp)
newSet->Belt.Set.Dict.toArray /* [1, 2, 3, 4, 5] */
remove
let remove: (
t<'value, 'id>,
'value,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
Removes element from set. If element did not exist in set, value is unchanged.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([2, 3, 1, 4, 5], ~cmp=IntCmp.cmp)
let s1 = s0->Belt.Set.Dict.remove(1, ~cmp=IntCmp.cmp)
let s2 = s1->Belt.Set.Dict.remove(3, ~cmp=IntCmp.cmp)
let s3 = s2->Belt.Set.Dict.remove(3, ~cmp=IntCmp.cmp)
s1->Belt.Set.Dict.toArray /* [2,3,4,5] */
s2->Belt.Set.Dict.toArray /* [2,4,5] */
s2 == s3 /* true */
removeMany
let removeMany: (
t<'value, 'id>,
array<'value>,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
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.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let set = Belt.Set.Dict.fromArray([1, 2, 3, 4], ~cmp=IntCmp.cmp)
let newSet = set->Belt.Set.Dict.removeMany([5, 4, 3, 2, 1], ~cmp=IntCmp.cmp)
newSet->Belt.Set.Dict.toArray /* [] */
union
let union: (
t<'value, 'id>,
t<'value, 'id>,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
Returns union of two sets.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3, 5, 6], ~cmp=IntCmp.cmp)
let s1 = Belt.Set.Dict.fromArray([5, 2, 3, 1, 5, 4], ~cmp=IntCmp.cmp)
let union = Belt.Set.Dict.union(s0, s1, ~cmp=IntCmp.cmp)
union->Belt.Set.Dict.toArray /* [1,2,3,4,5,6] */
intersect
let intersect: (
t<'value, 'id>,
t<'value, 'id>,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
Returns intersection of two sets.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3, 5, 6], ~cmp=IntCmp.cmp)
let s1 = Belt.Set.Dict.fromArray([5, 2, 3, 1, 5, 4], ~cmp=IntCmp.cmp)
let intersect = Belt.Set.Dict.intersect(s0, s1, ~cmp=IntCmp.cmp)
intersect->Belt.Set.Dict.toArray /* [2,3,5] */
diff
let diff: (
t<'value, 'id>,
t<'value, 'id>,
~cmp: cmp<'value, 'id>,
) => t<'value, 'id>
Returns elements from first set, not existing in second set.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3, 5, 6], ~cmp=IntCmp.cmp)
let s1 = Belt.Set.Dict.fromArray([5, 2, 3, 1, 5, 4], ~cmp=IntCmp.cmp)
let diff1 = Belt.Set.Dict.diff(s0, s1, ~cmp=IntCmp.cmp)
let diff2 = Belt.Set.Dict.diff(s1, s0, ~cmp=IntCmp.cmp)
diff1->Belt.Set.Dict.toArray /* [6] */
diff2->Belt.Set.Dict.toArray /* [1,4] */
subset
let subset: (
t<'value, 'id>,
t<'value, 'id>,
~cmp: cmp<'value, 'id>,
) => bool
Checks if second set is subset of first set.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3, 5, 6], ~cmp=IntCmp.cmp)
let s1 = Belt.Set.Dict.fromArray([5, 2, 3, 1, 5, 4], ~cmp=IntCmp.cmp)
let s2 = Belt.Set.Dict.intersect(s0, s1, ~cmp=IntCmp.cmp)
Belt.Set.Dict.subset(s2, s0, ~cmp=IntCmp.cmp) /* true */
Belt.Set.Dict.subset(s2, s1, ~cmp=IntCmp.cmp) /* true */
Belt.Set.Dict.subset(s1, s0, ~cmp=IntCmp.cmp) /* false */
cmp
let cmp: (
t<'value, 'id>,
t<'value, 'id>,
~cmp: cmp<'value, 'id>,
) => 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
let eq: (
t<'value, 'id>,
t<'value, 'id>,
~cmp: cmp<'value, 'id>,
) => bool
Checks if two sets are equal.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3], ~cmp=IntCmp.cmp)
let s1 = Belt.Set.Dict.fromArray([3, 2, 5], ~cmp=IntCmp.cmp)
Belt.Set.Dict.eq(s0, s1, ~cmp=IntCmp.cmp) /* true */
forEachU
let forEachU: (t<'value, 'id>, 'value => unit) => unit
Same as forEach but takes uncurried functon.
forEach
let forEach: (t<'value, 'id>, 'value => unit) => unit
Applies function f
in turn to all elements of set in increasing order.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3, 5, 6], ~cmp=IntCmp.cmp)
let acc = ref(list{})
s0->Belt.Set.Dict.forEach(x => acc := Belt.List.add(acc.contents, x))
acc /* [6,5,3,2] */
reduceU
let reduceU: (t<'value, 'id>, 'a, ('a, 'value) => 'a) => 'a
reduce
let reduce: (t<'value, 'id>, '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.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([5, 2, 3, 5, 6], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.reduce(list{}, (acc, element) => acc->Belt.List.add(element)) /* [6,5,3,2] */
everyU
let everyU: (t<'value, 'id>, 'value => bool) => bool
every
let every: (t<'value, 'id>, 'value => bool) => bool
Checks if all elements of the set satisfy the predicate. Order unspecified.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let isEven = x => mod(x, 2) == 0
let s0 = Belt.Set.Dict.fromArray([2, 4, 6, 8], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.every(isEven) /* true */
someU
let someU: (t<'value, 'id>, 'value => bool) => bool
some
let some: (t<'value, 'id>, 'value => bool) => bool
Checks if at least one element of the set satisfies the predicate.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let isOdd = x => mod(x, 2) != 0
let s0 = Belt.Set.Dict.fromArray([1, 2, 4, 6, 8], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.some(isOdd) /* true */
keepU
let keepU: (t<'value, 'id>, 'value => bool) => t<'value, 'id>
keep
let keep: (t<'value, 'id>, 'value => bool) => t<'value, 'id>
Returns the set of all elements that satisfy the predicate.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let isEven = x => mod(x, 2) == 0
let s0 = Belt.Set.Dict.fromArray([1, 2, 3, 4, 5], ~cmp=IntCmp.cmp)
let s1 = s0->Belt.Set.Dict.keep(isEven)
s1->Belt.Set.Dict.toArray /* [2,4] */
partitionU
let partitionU: (
t<'value, 'id>,
'value => bool,
) => (t<'value, 'id>, t<'value, 'id>)
partition
let partition: (
t<'value, 'id>,
'value => bool,
) => (t<'value, 'id>, t<'value, 'id>)
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.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let isOdd = x => mod(x, 2) != 0
let s0 = Belt.Set.Dict.fromArray([1, 2, 3, 4, 5], ~cmp=IntCmp.cmp)
let (s1, s2) = s0->Belt.Set.Dict.partition(isOdd)
s1->Belt.Set.Dict.toArray /* [1,3,5] */
s2->Belt.Set.Dict.toArray /* [2,4] */
size
let size: t<'value, 'id> => int
Returns size of the set.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([1, 2, 3, 4], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.size /* 4 */
toList
let toList: t<'value, 'id> => list<'value>
Returns list of ordered set elements.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([3, 2, 1, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.toList /* [1,2,3,5] */
toArray
let toArray: t<'value, 'id> => array<'value>
Returns array of ordered set elements.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([3, 2, 1, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.toArray /* [1,2,3,5] */
minimum
let minimum: t<'value, 'id> => option<'value>
Returns minimum value of the collection. None
if collection is empty.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.empty
let s1 = Belt.Set.Dict.fromArray([3, 2, 1, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.minimum /* None */
s1->Belt.Set.Dict.minimum /* Some(1) */
minUndefined
let minUndefined: t<'value, 'id> => Js.undefined<'value>
Returns minimum value of the collection. undefined
if collection is empty.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.empty
let s1 = Belt.Set.Dict.fromArray([3, 2, 1, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.minUndefined /* undefined */
s1->Belt.Set.Dict.minUndefined /* 1 */
maximum
let maximum: t<'value, 'id> => option<'value>
Returns maximum value of the collection. None
if collection is empty.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.empty
let s1 = Belt.Set.Dict.fromArray([3, 2, 1, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.maximum /* None */
s1->Belt.Set.Dict.maximum /* Some(5) */
maxUndefined
let maxUndefined: t<'value, 'id> => Js.undefined<'value>
Returns maximum value of the collection. undefined
if collection is empty.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.empty
let s1 = Belt.Set.Dict.fromArray([3, 2, 1, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.maxUndefined /* undefined */
s1->Belt.Set.Dict.maxUndefined /* 5 */
get
let get: (
t<'value, 'id>,
'value,
~cmp: cmp<'value, 'id>,
) => 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.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([1, 2, 3, 4, 5], ~cmp=IntCmp.cmp)
s0->Belt.Set.Dict.get(3, ~cmp=IntCmp.cmp) /* Some(3) */
s0->Belt.Set.Dict.get(20, ~cmp=IntCmp.cmp) /* None */
getUndefined
let getUndefined: (
t<'value, 'id>,
'value,
~cmp: cmp<'value, 'id>,
) => Js.undefined<'value>
Same as get but returns undefined
when element does not exist.
getExn
let getExn: (t<'value, 'id>, 'value, ~cmp: cmp<'value, 'id>) => 'value
Same as get but raise when element does not exist.
split
let split: (
t<'value, 'id>,
'value,
~cmp: cmp<'value, 'id>,
) => ((t<'value, 'id>, t<'value, 'id>), bool)
Returns a tuple ((smaller, larger), present)
, present
is true when element exist in set.
Examples
RESCRIPTmodule IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = Pervasives.compare
})
let s0 = Belt.Set.Dict.fromArray([1, 2, 3, 4, 5], ~cmp=IntCmp.cmp)
let ((smaller, larger), present) = s0->Belt.Set.Dict.split(3, ~cmp=IntCmp.cmp)
present /* true */
smaller->Belt.Set.Dict.toArray /* [1,2] */
larger->Belt.Set.Dict.toArray /* [4,5] */
checkInvariantInternal
let checkInvariantInternal: t<'a, 'b> => unit
raise when invariant is not held