Skip to content

Commit 8d81863

Browse files
authored
Add Set functions (#247)
Set.difference Set.intersection Set.union Set.symmetricDifference Set.isSubsetOf Set.isSupersetOf Set.isDisjointFrom Set.toArray
1 parent 0de657a commit 8d81863

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Next version
44

5+
- Add `difference`, `intersection`, `union`, `symmetricDifference`, `isSubsetOf`, `isSupersetOf`, `isDisjointFrom`, `toArray` functions to `Set`. https://github.com/rescript-association/rescript-core/pull/247
6+
57
## 1.6.0
68

79
- Optimize compare and equal functions. https://github.com/rescript-association/rescript-core/pull/238

src/Core__Set.res

+10
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ type t<'a> = Js.Set.t<'a>
1515
@send external forEach: (t<'a>, 'a => unit) => unit = "forEach"
1616

1717
@send external values: t<'a> => Core__Iterator.t<'a> = "values"
18+
19+
@send external difference: (t<'a>, t<'a>) => t<'a> = "difference"
20+
@send external intersection: (t<'a>, t<'a>) => t<'a> = "intersection"
21+
@send external union: (t<'a>, t<'a>) => t<'a> = "union"
22+
@send external symmetricDifference: (t<'a>, t<'a>) => t<'a> = "symmetricDifference"
23+
@send external isSubsetOf: (t<'a>, t<'a>) => bool = "isSubsetOf"
24+
@send external isSupersetOf: (t<'a>, t<'a>) => bool = "isSupersetOf"
25+
@send external isDisjointFrom: (t<'a>, t<'a>) => bool = "isDisjointFrom"
26+
27+
external toArray: t<'a> => array<'a> = "Array.from"

src/Core__Set.resi

+133
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ external fromIterator: Core__Iterator.t<'a> => t<'a> = "Set"
6868
/**
6969
Returns the size, the number of unique values, of the set.
7070

71+
See [`Set.prototype.size`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/size) on MDN.
72+
7173
## Examples
7274
```rescript
7375
let set = Set.make()
@@ -85,6 +87,8 @@ external size: t<'a> => int = "size"
8587
/**
8688
Clears all entries in the set.
8789

90+
See [`Set.clear`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/clear) on MDN.
91+
8892
## Examples
8993
```rescript
9094
let set = Set.make()
@@ -102,6 +106,8 @@ external clear: t<'a> => unit = "clear"
102106
/**
103107
Adds a new value to the set.
104108

109+
See [`Set.add`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/add) on MDN.
110+
105111
## Examples
106112
```rescript
107113
let set = Set.make()
@@ -114,6 +120,8 @@ external add: (t<'a>, 'a) => unit = "add"
114120
/**
115121
Deletes the provided `value` from the set. Returns a `bool` for whether the value existed, and was deleted.
116122

123+
See [`Set.delete`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/delete) on MDN.
124+
117125
## Examples
118126
```rescript
119127
let set = Set.make()
@@ -131,6 +139,8 @@ external delete: (t<'a>, 'a) => bool = "delete"
131139
/**
132140
Checks whether the set has a specific value.
133141

142+
See [`Set.has`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has) on MDN.
143+
134144
## Examples
135145
```rescript
136146
let set = Set.make()
@@ -148,6 +158,8 @@ external has: (t<'a>, 'a) => bool = "has"
148158
/**
149159
Iterates through all values of the set.
150160

161+
See [`Set.forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/forEach) on MDN.
162+
151163
## Examples
152164
```rescript
153165
let set = Set.make()
@@ -165,6 +177,8 @@ external forEach: (t<'a>, 'a => unit) => unit = "forEach"
165177
/**
166178
Returns an iterator that holds all values of the set.
167179

180+
See [`Set.values`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/values) on MDN.
181+
168182
## Examples
169183
```rescript
170184
let set = Set.make()
@@ -183,3 +197,122 @@ Console.log(set->Set.values->Iterator.toArray)
183197
*/
184198
@send
185199
external values: t<'a> => Core__Iterator.t<'a> = "values"
200+
201+
/**
202+
Returns a new set with the values of the set that are not in the other set.
203+
204+
See [`Set.difference`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/difference) on MDN.
205+
206+
## Examples
207+
```rescript
208+
let set1 = Set.fromArray(["apple", "orange", "banana"])
209+
let set2 = Set.fromArray(["apple", "banana", "pear"])
210+
set1->Set.difference(set2) // Set.fromArray(["orange"])
211+
```
212+
*/
213+
@send
214+
external difference: (t<'a>, t<'a>) => t<'a> = "difference"
215+
216+
/**
217+
Returns a new set with the values containing the values which are in either the set, but not in both.
218+
219+
See [`Set.symmetricDifference`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/symmetricDifference) on MDN.
220+
221+
## Examples
222+
```rescript
223+
let set1 = Set.fromArray(["apple", "orange", "banana"])
224+
let set2 = Set.fromArray(["apple", "banana", "pear"])
225+
set1->Set.symmetricDifference(set2) // Set.fromArray(["orange", "pear"])
226+
```
227+
228+
*/
229+
@send
230+
external symmetricDifference: (t<'a>, t<'a>) => t<'a> = "symmetricDifference"
231+
232+
/**
233+
Returns a new set with the values containing the values which are in both the set and the other set.
234+
235+
See [`Set.intersection`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection) on MDN.
236+
237+
## Examples
238+
```rescript
239+
let set1 = Set.fromArray(["apple", "orange", "banana"])
240+
let set2 = Set.fromArray(["apple", "banana", "pear"])
241+
set1->Set.intersection(set2) // Set.fromArray(["apple", "banana"])
242+
```
243+
*/
244+
@send
245+
external intersection: (t<'a>, t<'a>) => t<'a> = "intersection"
246+
247+
/**
248+
Returns a bool indicating if this set has no elements in common with the given set.
249+
250+
See [`Set.isDisjointFrom`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/isDisjointFrom) on MDN.
251+
252+
## Examples
253+
```rescript
254+
let set1 = Set.fromArray(["apple", "orange", "banana"])
255+
let set2 = Set.fromArray(["kiwi", "melon", "pear"])
256+
set1->Set.isDisjointFrom(set2) // true
257+
```
258+
*/
259+
@send
260+
external isDisjointFrom: (t<'a>, t<'a>) => bool = "isDisjointFrom"
261+
262+
/**
263+
Returns a bool indicating if the all values in the set are in the given set.
264+
265+
See [`Set.isSubsetOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/isSubsetOf) on MDN.
266+
267+
## Examples
268+
```rescript
269+
let set1 = Set.fromArray(["apple", "banana"])
270+
let set2 = Set.fromArray(["apple", "banana", "pear"])
271+
set1->Set.isSubsetOf(set2) // true
272+
```
273+
*/
274+
@send
275+
external isSubsetOf: (t<'a>, t<'a>) => bool = "isSubsetOf"
276+
277+
/**
278+
Returns a bool indicating if the all values in the given set are in the set.
279+
280+
See [`Set.isSupersetOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/isSupersetOf) on MDN.
281+
282+
## Examples
283+
```rescript
284+
let set1 = Set.fromArray(["apple", "banana", "pear"])
285+
let set2 = Set.fromArray(["apple", "banana"])
286+
set1->Set.isSupersetOf(set2) // true
287+
```
288+
*/
289+
@send
290+
external isSupersetOf: (t<'a>, t<'a>) => bool = "isSupersetOf"
291+
292+
/**
293+
Returns a new set with the values of the set that are in both the set and the other set.
294+
295+
See [`Set.union`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/union) on MDN.
296+
297+
## Examples
298+
```rescript
299+
let set1 = Set.fromArray(["apple", "orange", "banana"])
300+
let set2 = Set.fromArray(["apple", "banana", "pear"])
301+
set1->Set.union(set2) // Set.fromArray(["apple", "orange", "banana", "pear"])
302+
```
303+
*/
304+
@send
305+
external union: (t<'a>, t<'a>) => t<'a> = "union"
306+
307+
/**
308+
`toArray(set)` returns an array of all values of the set.
309+
310+
See [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) on MDN.
311+
312+
## Examples
313+
```rescript
314+
let set = Set.fromArray(["apple", "orange", "apple", "banana"])
315+
set->Set.toArray // ["apple", "orange", "banana"]
316+
```
317+
*/
318+
external toArray: t<'a> => array<'a> = "Array.from"

0 commit comments

Comments
 (0)