This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Core Module
Overview
Core
submodules
  • Array
  • ArrayBuffer
  • AsyncIterator
  • BigInt
  • BigInt64Array
    • Constants
    BigUint64Array
    • Constants
  • Console
  • DataView
  • Date
    • UTC
  • Dict
  • Error
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • Exn
  • Float
    • Constants
    Float32Array
    • Constants
    Float64Array
    • Constants
    Int
    • Constants
    Int16Array
    • Constants
    Int32Array
    • Constants
    Int8Array
    • Constants
  • Internal
  • Intl
    • Segments
    • Segmenter
    • RelativeTimeFormat
    • PluralRules
    • NumberFormat
      • Grouping
    • Locale
    • ListFormat
    • DateTimeFormat
    • Collator
    • Common
  • Iterator
  • JSON
    • Decode
    • Encode
    • Classify
  • List
  • Map
  • MapperRt
  • Math
    • Int
    • Constants
  • Null
  • Nullable
  • Object
  • Option
  • Ordering
  • Promise
  • Re
    • Result
    RegExp
    • Result
  • Result
  • Set
    • t
      t
    • v
      make
    • v
      fromArray
    • v
      fromIterator
    • v
      size
    • v
      clear
    • v
      add
    • v
      delete
    • v
      has
    • v
      forEach
    • v
      values
  • String
  • Symbol
  • Type
    • Classify
  • TypedArray
  • Uint16Array
    • Constants
    Uint32Array
    • Constants
    Uint8Array
    • Constants
    Uint8ClampedArray
    • Constants
  • WeakMap
  • WeakSet
  • API / Core / Set

    Set

    Bindings to the mutable JavaScript Set.

    See Set on MDN.

    t

    RESCRIPT
    type t<'a> = Js.Set.t<'a>

    Type representing an instance of Set.

    make

    RESCRIPT
    let make: unit => t<'a>

    Creates a new, mutable JavaScript Set. A Set is a collection of unique values.

    See Set on MDN.

    Examples

    RESCRIPT
    // You can annotate the type of your set if you want to let mySet: Set.t<string> = Set.make() // Or you can let ReScript infer what's in your Set let set = Set.make() set->Set.add("Fine name") // Inferred as Set.t<string>

    Alternatives

    A JavaScript Set is mutable. If you're looking for an immutable alternative, check out Belt.Set.

    fromArray

    RESCRIPT
    let fromArray: array<'a> => t<'a>

    Turns an array of values into a Set. Meaning only unique values are preserved.

    Examples

    RESCRIPT
    type languages = ReScript | JavaScript | TypeScript let languageRank = [ReScript, JavaScript, TypeScript] let set = Set.fromArray(languageRank) // Set.t<languages> switch set->Set.has(ReScript) { | true => Console.log("Yay, ReScript is in there!") | false => Console.log("Uh-oh, something is _terribly_ wrong with this program... abort.") }

    fromIterator

    RESCRIPT
    let fromIterator: Core__Iterator.t<'a> => t<'a>

    Turns an iterator into a Set.

    Examples

    RESCRIPT
    // Let's pretend we have an interator @val external someIterator: Iterator.t<int> = "someIterator" let set = Set.fromIterator(someIterator) // Set.t<int>

    size

    RESCRIPT
    let size: t<'a> => int

    Returns the size, the number of unique values, of the set.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someValue") set->Set.add("someValue") set->Set.add("someValue2") let size = set->Set.size // 2

    clear

    RESCRIPT
    let clear: t<'a> => unit

    Clears all entries in the set.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someKey") set->Set.size // 1 set->Set.clear set->Set.size // 0

    add

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

    Adds a new value to the set.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someValue")

    delete

    RESCRIPT
    let delete: (t<'a>, 'a) => bool

    Deletes the provided value from the set. Returns a bool for whether the value existed, and was deleted.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someValue") let didDeleteValue = set->Set.delete("someValue") Console.log(didDeleteValue) // Logs `true` to the console, becuase the set had the value, so it was successfully deleted let didDeleteValue = set->Set.delete("someNonExistantKey") Console.log(didDeleteValue) // Logs `false` to the console, becuase the value did not exist in the set

    has

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

    Checks whether the set has a specific value.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someValue") switch set->Set.has("someValue") { | false => Console.log("Nope, didn't have it.") | true => Console.log("Yay, we have the value!") }

    forEach

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

    Iterates through all values of the set.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someValue") set->Set.add("someValue2") set->Set.forEach(value => { Console.log(value) })

    values

    RESCRIPT
    let values: t<'a> => Core__Iterator.t<'a>

    Returns an iterator that holds all values of the set.

    Examples

    RESCRIPT
    let set = Set.make() set->Set.add("someValue") set->Set.add("anotherValue") let values = set->Set.values // Logs the first value Console.log(Iterator.next(values).value) // You can also turn the iterator into an array. // Remember that an iterator consumes values. We'll need a fresh values iterator to get an array of all values, since we consumed a value via `next` above already. Console.log(set->Set.values->Iterator.toArray)
    Types and values
    • t
      t
    • v
      make
    • v
      fromArray
    • v
      fromIterator
    • v
      size
    • v
      clear
    • v
      add
    • v
      delete
    • v
      has
    • v
      forEach
    • v
      values

    © 2024 The ReScript Project

    Software and assets distribution powered by KeyCDN.

    About
    • Community
    • ReScript Association
    Find us on