Set

Bindings to the mutable JavaScript Set.

See Set on MDN.

add

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

Adds a new value to the set.

See Set.add on MDN.

Examples

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

clear

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

Clears all entries in the set.

See Set.clear on MDN.

Examples

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

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.

See Set.delete on MDN.

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

difference

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

Returns a new set with the values of the set that are not in the other set.

See Set.difference on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "orange", "banana"]) let set2 = Set.fromArray(["apple", "banana", "pear"]) set1->Set.difference(set2) // Set.fromArray(["orange"])

forEach

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

Iterates through all values of the set.

See Set.forEach on MDN.

Examples

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

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: Iterator.t<'a> => t<'a>

Turns an iterator into a Set.

Examples

RESCRIPT
// Let's pretend we have an interator let iterator: Iterator.t<string> = %raw(` (() => { var array1 = ['a', 'b', 'c']; var iterator1 = array1[Symbol.iterator](); return iterator1 })() `) iterator ->Set.fromIterator ->Set.size == 3

has

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

Checks whether the set has a specific value.

See Set.has on MDN.

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!") }

ignore

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

ignore(set) ignores the provided set and returns unit.

This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.

intersection

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

Returns a new set with the values containing the values which are in both the set and the other set.

See Set.intersection on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "orange", "banana"]) let set2 = Set.fromArray(["apple", "banana", "pear"]) set1->Set.intersection(set2) // Set.fromArray(["apple", "banana"])

isDisjointFrom

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

Returns a bool indicating if this set has no elements in common with the given set.

See Set.isDisjointFrom on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "orange", "banana"]) let set2 = Set.fromArray(["kiwi", "melon", "pear"]) set1->Set.isDisjointFrom(set2) // true

isEmpty

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

isEmpty(set) returns true if the set has no values, false otherwise.

Examples

RESCRIPT
let emptySet = Set.make() emptySet->Set.isEmpty->assertEqual(true) let set = Set.make() set->Set.add("someValue") set->Set.isEmpty->assertEqual(false) // After clearing the set set->Set.clear set->Set.isEmpty->assertEqual(true)

isSubsetOf

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

Returns a bool indicating if the all values in the set are in the given set.

See Set.isSubsetOf on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "banana"]) let set2 = Set.fromArray(["apple", "banana", "pear"]) set1->Set.isSubsetOf(set2) // true

isSupersetOf

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

Returns a bool indicating if the all values in the given set are in the set.

See Set.isSupersetOf on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "banana", "pear"]) let set2 = Set.fromArray(["apple", "banana"]) set1->Set.isSupersetOf(set2) // true

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.

size

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

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

See Set.prototype.size on MDN.

Examples

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

symmetricDifference

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

Returns a new set with the values containing the values which are in either the set, but not in both.

See Set.symmetricDifference on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "orange", "banana"]) let set2 = Set.fromArray(["apple", "banana", "pear"]) set1->Set.symmetricDifference(set2) // Set.fromArray(["orange", "pear"])

t

RESCRIPT
type t<'a>

Type representing an instance of Set.

toArray

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

toArray(set) returns an array of all values of the set.

See Array.from on MDN.

Examples

RESCRIPT
let set = Set.fromArray(["apple", "orange", "apple", "banana"]) set->Set.toArray // ["apple", "orange", "banana"]

union

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

Returns a new set with the values of the set that are in both the set and the other set.

See Set.union on MDN.

Examples

RESCRIPT
let set1 = Set.fromArray(["apple", "orange", "banana"]) let set2 = Set.fromArray(["apple", "banana", "pear"]) set1->Set.union(set2) // Set.fromArray(["apple", "orange", "banana", "pear"])

values

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

Returns an iterator that holds all values of the set.

See Set.values on MDN.

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)