forked from rescript-lang/rescript-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore__Set.resi
185 lines (145 loc) · 3.96 KB
/
Core__Set.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***
Bindings to the mutable JavaScript `Set`.
See [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) on MDN.
*/
/**
Type representing an instance of `Set`.
*/
type t<'a> = Js.Set.t<'a>
/**
Creates a new, mutable JavaScript `Set`. A `Set` is a collection of unique values.
See [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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`.
*/
@new
external make: unit => t<'a> = "Set"
/**
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.")
}
```
*/
@new
external fromArray: array<'a> => t<'a> = "Set"
/**
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>
```
*/
@new
external fromIterator: Core__Iterator.t<'a> => t<'a> = "Set"
/**
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
```
*/
@get
external size: t<'a> => int = "size"
/**
Clears all entries in the set.
## Examples
```rescript
let set = Set.make()
set->Set.add("someKey")
let size = set->Set.size // 1
set->Set.clear
let size = set->Set.size // 0
```
*/
@send
external clear: t<'a> => unit = "clear"
/**
Adds a new value to the set.
## Examples
```rescript
let set = Set.make()
set->Set.add("someValue")
```
*/
@send
external add: (t<'a>, 'a) => unit = "add"
/**
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
```
*/
@send
external delete: (t<'a>, 'a) => bool = "delete"
/**
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!")
}
```
*/
@send
external has: (t<'a>, 'a) => bool = "has"
/**
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)
})
```
*/
@send
external forEach: (t<'a>, 'a => unit) => unit = "forEach"
/**
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)
```
*/
@send
external values: t<'a> => Core__Iterator.t<'a> = "values"