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
RESCRIPTtype 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
RESCRIPTlet 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
RESCRIPTlet 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
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
RESCRIPTlet 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
RESCRIPTlet 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
RESCRIPTlet 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
RESCRIPTlet 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)