ReScript HomeReScript Home
DocsPlaygroundBlogCommunityPackages
  • Playground
  • Blog
  • Community
  • Packages
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
v11v9.1 - v10.1v8.2 - v9.0v6.0 - v8.1
Overview
Belt
submodules
  • Array
  • Float
  • HashMap
    • String
    • Int
    HashSet
    • String
    • Int
    Id
    • MakeHashable
    • MakeHashableU
    • MakeComparable
    • MakeComparableU
  • Int
  • List
  • Map
    • Dict
    • String
    • Int
    MutableMap
    • String
    • Int
  • MutableQueue
  • MutableSet
    • String
    • Int
  • MutableStack
  • Option
  • Range
  • Result
  • Set
    • Dict
    • String
    • Int
    SortArray
    • String
    • Int
    Docs / API / Mutableset

    MutableSet

    A mutable sorted set module which allows customized compare behavior. The implementation uses balanced binary trees, and therefore searching and insertion take time logarithmic in the size of the map.

    It also has two specialized inner modules Belt.MutableSet.Int and Belt.MutableSet.String - This module separates data from function which is more verbose but slightly more efficient

    Examples

    RESCRIPT
    module PairComparator = Belt.Id.MakeComparable({ type t = (int, int) let cmp = ((a0, a1), (b0, b1)) => switch Pervasives.compare(a0, b0) { | 0 => Pervasives.compare(a1, b1) | c => c } }) let mySet = Belt.MutableSet.make(~id=module(PairComparator)) mySet->Belt.MutableSet.add((1, 2))

    add

    RESCRIPT
    let add: (t<'value, 'id>, 'value) => unit

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.make(~id=module(IntCmp)) s0->Belt.MutableSet.add(1) s0->Belt.MutableSet.add(2) s0->Belt.MutableSet.add(2) s0->Belt.MutableSet.toArray == [1, 2]

    addCheck

    RESCRIPT
    let addCheck: (t<'value, 'id>, 'value) => bool

    checkInvariantInternal

    RESCRIPT
    let checkInvariantInternal: t<'a, 'b> => unit

    throw when invariant is not held

    cmp

    RESCRIPT
    let cmp: (t<'value, 'id>, t<'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.

    copy

    RESCRIPT
    let copy: t<'value, 'id> => t<'value, 'id>

    Returns copy of a set.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([1, 3, 2, 4], ~id=module(IntCmp)) let copied = s0->Belt.MutableSet.copy copied->Belt.MutableSet.toArray == [1, 2, 3, 4]

    diff

    RESCRIPT
    let diff: (t<'value, 'id>, t<'value, 'id>) => t<'value, 'id>

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3, 5, 6], ~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([5, 2, 3, 1, 5, 4], ~id=module(IntCmp)) Belt.MutableSet.toArray(Belt.MutableSet.diff(s0, s1)) == [6] Belt.MutableSet.toArray(Belt.MutableSet.diff(s1, s0)) == [1, 4]

    eq

    RESCRIPT
    let eq: (t<'value, 'id>, t<'value, 'id>) => bool

    Checks if two sets are equal.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3], ~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([3, 2, 5], ~id=module(IntCmp)) Belt.MutableSet.eq(s0, s1) == true

    every

    RESCRIPT
    let every: (t<'value, 'id>, 'value => bool) => bool

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let isEven = x => mod(x, 2) == 0 let s0 = Belt.MutableSet.fromArray([2, 4, 6, 8], ~id=module(IntCmp)) s0->Belt.MutableSet.every(isEven) == true

    everyU

    Deprecated

    Use every instead

    RESCRIPT
    let everyU: (t<'value, 'id>, 'value => bool) => bool

    forEach

    RESCRIPT
    let forEach: (t<'value, 'id>, 'value => unit) => unit

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3, 5, 6], ~id=module(IntCmp)) let acc = ref(list{}) s0->Belt.MutableSet.forEach(x => acc := Belt.List.add(acc.contents, x)) acc.contents == list{6, 5, 3, 2}

    forEachU

    Deprecated

    Use forEach instead

    RESCRIPT
    let forEachU: (t<'value, 'id>, 'value => unit) => unit

    Same as Belt.MutableSet.forEach but takes uncurried functon.

    fromArray

    RESCRIPT
    let fromArray: (array<'value>, ~id: id<'value, 'id>) => t<'value, 'id>

    Creates new set from array of elements.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([1, 3, 2, 4], ~id=module(IntCmp)) s0->Belt.MutableSet.toArray == [1, 2, 3, 4]

    fromSortedArrayUnsafe

    RESCRIPT
    let fromSortedArrayUnsafe: (array<'value>, ~id: id<'value, 'id>) => t<'value, 'id>

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

    get

    RESCRIPT
    let get: (t<'value, 'id>, '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.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([1, 2, 3, 4, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.get(3) == Some(3) s0->Belt.MutableSet.get(20) == None

    getExn

    RESCRIPT
    let getExn: (t<'value, 'id>, 'value) => 'value

    Same as Belt.MutableSet.get but throw when element does not exist.

    getOrThrow

    RESCRIPT
    let getOrThrow: (t<'value, 'id>, 'value) => 'value

    Same as Belt.MutableSet.get but throw when element does not exist.

    getUndefined

    RESCRIPT
    let getUndefined: (t<'value, 'id>, 'value) => Js.undefined<'value>

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

    has

    RESCRIPT
    let has: (t<'value, 'id>, 'value) => bool

    Checks if element exists in set.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let set = Belt.MutableSet.fromArray([1, 4, 2, 5], ~id=module(IntCmp)) set->Belt.MutableSet.has(3) == false set->Belt.MutableSet.has(1) == true

    id

    RESCRIPT
    type id<'value, 'id> = Belt_Id.comparable<'value, 'id>

    The identity needed for making a set from scratch

    intersect

    RESCRIPT
    let intersect: (t<'value, 'id>, t<'value, 'id>) => t<'value, 'id>

    Returns intersection of two sets.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3, 5, 6], ~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([5, 2, 3, 1, 5, 4], ~id=module(IntCmp)) let intersect = Belt.MutableSet.intersect(s0, s1) intersect->Belt.MutableSet.toArray == [2, 3, 5]

    isEmpty

    RESCRIPT
    let isEmpty: t<'a, 'b> => bool

    Checks if set is empty.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let empty = Belt.MutableSet.fromArray([], ~id=module(IntCmp)) let notEmpty = Belt.MutableSet.fromArray([1], ~id=module(IntCmp)) Belt.MutableSet.isEmpty(empty) == true Belt.MutableSet.isEmpty(notEmpty) == false

    keep

    RESCRIPT
    let keep: (t<'value, 'id>, 'value => bool) => t<'value, 'id>

    Returns the set of all elements that satisfy the predicate.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let isEven = x => mod(x, 2) == 0 let s0 = Belt.MutableSet.fromArray([1, 2, 3, 4, 5], ~id=module(IntCmp)) let s1 = s0->Belt.MutableSet.keep(isEven) s1->Belt.MutableSet.toArray == [2, 4]

    keepU

    Deprecated

    Use keep instead

    RESCRIPT
    let keepU: (t<'value, 'id>, 'value => bool) => t<'value, 'id>

    make

    RESCRIPT
    let make: (~id: id<'value, 'id>) => t<'value, 'id>

    Creates a new set by taking in the comparator

    maximum

    RESCRIPT
    let maximum: t<'value, 'id> => option<'value>

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.make(~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([3, 2, 1, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.maximum == None s1->Belt.MutableSet.maximum == Some(5)

    maxUndefined

    RESCRIPT
    let maxUndefined: t<'value, 'id> => Js.undefined<'value>

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.make(~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([3, 2, 1, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.maxUndefined == Js.undefined s1->Belt.MutableSet.maxUndefined == Js.Undefined.return(5)

    mergeMany

    RESCRIPT
    let mergeMany: (t<'value, 'id>, array<'value>) => unit

    Adds each element of array to set.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let set = Belt.MutableSet.make(~id=module(IntCmp)) set->Belt.MutableSet.mergeMany([5, 4, 3, 2, 1]) set->Belt.MutableSet.toArray == [1, 2, 3, 4, 5]

    minimum

    RESCRIPT
    let minimum: t<'value, 'id> => option<'value>

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.make(~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([3, 2, 1, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.minimum == None s1->Belt.MutableSet.minimum == Some(1)

    minUndefined

    RESCRIPT
    let minUndefined: t<'value, 'id> => Js.undefined<'value>

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.make(~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([3, 2, 1, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.minUndefined == Js.undefined s1->Belt.MutableSet.minUndefined == Js.Undefined.return(1)

    partition

    RESCRIPT
    let partition: ( t<'value, 'id>, 'value => bool, ) => (t<'value, 'id>, t<'value, 'id>)

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let isOdd = x => mod(x, 2) != 0 let s0 = Belt.MutableSet.fromArray([1, 2, 3, 4, 5], ~id=module(IntCmp)) let (s1, s2) = s0->Belt.MutableSet.partition(isOdd) s1->Belt.MutableSet.toArray == [1, 3, 5] s2->Belt.MutableSet.toArray == [2, 4]

    partitionU

    Deprecated

    Use partition instead

    RESCRIPT
    let partitionU: ( t<'value, 'id>, 'value => bool, ) => (t<'value, 'id>, t<'value, 'id>)

    reduce

    RESCRIPT
    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

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3, 5, 6], ~id=module(IntCmp)) s0->Belt.MutableSet.reduce(list{}, (acc, element) => acc->Belt.List.add(element)) == list{6, 5, 3, 2}

    reduceU

    Deprecated

    Use reduce instead

    RESCRIPT
    let reduceU: (t<'value, 'id>, 'a, ('a, 'value) => 'a) => 'a

    remove

    RESCRIPT
    let remove: (t<'value, 'id>, 'value) => unit

    Removes element from set. If element did not exist in set, value is unchanged.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([2, 3, 1, 4, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.remove(1) s0->Belt.MutableSet.remove(3) s0->Belt.MutableSet.remove(3) s0->Belt.MutableSet.toArray == [2, 4, 5]

    removeCheck

    RESCRIPT
    let removeCheck: (t<'value, 'id>, 'value) => bool

    removeMany

    RESCRIPT
    let removeMany: (t<'value, 'id>, array<'value>) => unit

    Removes each element of array from set.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let set = Belt.MutableSet.fromArray([1, 2, 3, 4], ~id=module(IntCmp)) set->Belt.MutableSet.removeMany([5, 4, 3, 2, 1]) set->Belt.MutableSet.toArray == []

    size

    RESCRIPT
    let size: t<'value, 'id> => int

    Returns size of the set.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([1, 2, 3, 4], ~id=module(IntCmp)) s0->Belt.MutableSet.size == 4

    some

    RESCRIPT
    let some: (t<'value, 'id>, 'value => bool) => bool

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let isOdd = x => mod(x, 2) != 0 let s0 = Belt.MutableSet.fromArray([1, 2, 4, 6, 8], ~id=module(IntCmp)) s0->Belt.MutableSet.some(isOdd) == true

    someU

    Deprecated

    Use some instead

    RESCRIPT
    let someU: (t<'value, 'id>, 'value => bool) => bool

    split

    RESCRIPT
    let split: ( t<'value, 'id>, 'value, ) => ((t<'value, 'id>, t<'value, 'id>), bool)

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

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([1, 2, 3, 4, 5], ~id=module(IntCmp)) let ((smaller, larger), present) = s0->Belt.MutableSet.split(3) present == true smaller->Belt.MutableSet.toArray == [1, 2] larger->Belt.MutableSet.toArray == [4, 5]

    subset

    RESCRIPT
    let subset: (t<'value, 'id>, t<'value, 'id>) => bool

    Checks if second set is subset of first set.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3, 5, 6], ~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([5, 2, 3, 1, 5, 4], ~id=module(IntCmp)) let s2 = Belt.MutableSet.intersect(s0, s1) Belt.MutableSet.subset(s2, s0) == true Belt.MutableSet.subset(s2, s1) == true Belt.MutableSet.subset(s1, s0) == false

    t

    RESCRIPT
    type t<'value, 'identity>

    'value is the element type

    'identity the identity of the collection

    toArray

    RESCRIPT
    let toArray: t<'value, 'id> => array<'value>

    Returns array of ordered set elements.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([3, 2, 1, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.toArray == [1, 2, 3, 5]

    toList

    RESCRIPT
    let toList: t<'value, 'id> => list<'value>

    Returns list of ordered set elements.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([3, 2, 1, 5], ~id=module(IntCmp)) s0->Belt.MutableSet.toList == list{1, 2, 3, 5}

    union

    RESCRIPT
    let union: (t<'value, 'id>, t<'value, 'id>) => t<'value, 'id>

    Returns union of two sets.

    Examples

    RESCRIPT
    module IntCmp = Belt.Id.MakeComparable({ type t = int let cmp = Pervasives.compare }) let s0 = Belt.MutableSet.fromArray([5, 2, 3, 5, 6], ~id=module(IntCmp)) let s1 = Belt.MutableSet.fromArray([5, 2, 3, 1, 5, 4], ~id=module(IntCmp)) let union = Belt.MutableSet.union(s0, s1) union->Belt.MutableSet.toArray == [1, 2, 3, 4, 5, 6]
    Types and values
    • v
      add
    • v
      addCheck
    • v
      checkInvariantInternal
    • v
      cmp
    • v
      copy
    • v
      diff
    • v
      eq
    • v
      every
    • v
      everyU
      D
    • v
      forEach
    • v
      forEachU
      D
    • v
      fromArray
    • v
      fromSortedArrayUnsafe
    • v
      get
    • v
      getExn
    • v
      getOrThrow
    • v
      getUndefined
    • v
      has
    • t
      id
    • v
      intersect
    • v
      isEmpty
    • v
      keep
    • v
      keepU
      D
    • v
      make
    • v
      maximum
    • v
      maxUndefined
    • v
      mergeMany
    • v
      minimum
    • v
      minUndefined
    • v
      partition
    • v
      partitionU
      D
    • v
      reduce
    • v
      reduceU
      D
    • v
      remove
    • v
      removeCheck
    • v
      removeMany
    • v
      size
    • v
      some
    • v
      someU
      D
    • v
      split
    • v
      subset
    • t
      t
    • v
      toArray
    • v
      toList
    • v
      union

    © 2026 The ReScript Project

    About
    • Community
    • ReScript Association
    Find us on