Skip to content

Commit fe34e4b

Browse files
committed
Add Set functions
Set.difference Set.intersection Set.union Set.symmetricDifference Set.isSubsetOf Set.isSupersetOf Set.isDisjointFrom Set.toArray
1 parent 0de657a commit fe34e4b

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-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

+103
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,106 @@ Console.log(set->Set.values->Iterator.toArray)
183183
*/
184184
@send
185185
external values: t<'a> => Core__Iterator.t<'a> = "values"
186+
187+
/**
188+
Returns a new set with the values of the set that are not in the other set.
189+
190+
## Examples
191+
```rescript
192+
let set1 = Set.fromArray(["apple", "orange", "banana"])
193+
let set2 = Set.fromArray(["apple", "banana", "pear"])
194+
set1->Set.difference(set2) // Set.fromArray(["orange"])
195+
```
196+
*/
197+
@send
198+
external difference: (t<'a>, t<'a>) => t<'a> = "difference"
199+
200+
/**
201+
Returns a new set with the values containing the values which are in either the set, but not in both.
202+
203+
## Examples
204+
```rescript
205+
let set1 = Set.fromArray(["apple", "orange", "banana"])
206+
let set2 = Set.fromArray(["apple", "banana", "pear"])
207+
set1->Set.symmetricDifference(set2) // Set.fromArray(["orange", "pear"])
208+
```
209+
210+
*/
211+
@send
212+
external symmetricDifference: (t<'a>, t<'a>) => t<'a> = "symmetricDifference"
213+
214+
/**
215+
Returns a new set with the values containing the values which are in both the set and the other set.
216+
217+
## Examples
218+
```rescript
219+
let set1 = Set.fromArray(["apple", "orange", "banana"])
220+
let set2 = Set.fromArray(["apple", "banana", "pear"])
221+
set1->Set.intersection(set2) // Set.fromArray(["apple", "banana"])
222+
```
223+
*/
224+
@send
225+
external intersection: (t<'a>, t<'a>) => t<'a> = "intersection"
226+
227+
/**
228+
Returns a bool indicating if this set has no elements in common with the given set.
229+
230+
## Examples
231+
```rescript
232+
let set1 = Set.fromArray(["apple", "orange", "banana"])
233+
let set2 = Set.fromArray(["kiwi", "melon", "pear"])
234+
set1->Set.isDisjointFrom(set2) // true
235+
```
236+
*/
237+
@send
238+
external isDisjointFrom: (t<'a>, t<'a>) => bool = "isDisjointFrom"
239+
240+
/**
241+
Returns a bool indicating if the all values in the set are in the given set.
242+
243+
## Examples
244+
```rescript
245+
let set1 = Set.fromArray(["apple", "banana"])
246+
let set2 = Set.fromArray(["apple", "banana", "pear"])
247+
set1->Set.isSubsetOf(set2) // true
248+
```
249+
*/
250+
@send
251+
external isSubsetOf: (t<'a>, t<'a>) => bool = "isSubsetOf"
252+
253+
/**
254+
Returns a bool indicating if the all values in the given set are in the set.
255+
256+
## Examples
257+
```rescript
258+
let set1 = Set.fromArray(["apple", "banana", "pear"])
259+
let set2 = Set.fromArray(["apple", "banana"])
260+
set1->Set.isSupersetOf(set2) // true
261+
```
262+
*/
263+
@send
264+
external isSupersetOf: (t<'a>, t<'a>) => bool = "isSupersetOf"
265+
266+
/**
267+
Returns a new set with the values of the set that are in both the set and the other set.
268+
269+
## Examples
270+
```rescript
271+
let set1 = Set.fromArray(["apple", "orange", "banana"])
272+
let set2 = Set.fromArray(["apple", "banana", "pear"])
273+
set1->Set.union(set2) // Set.fromArray(["apple", "orange", "banana", "pear"])
274+
```
275+
*/
276+
@send
277+
external union: (t<'a>, t<'a>) => t<'a> = "union"
278+
279+
/**
280+
`toArray(set)` returns an array of all values of the set.
281+
282+
## Examples
283+
```rescript
284+
let set = Set.fromArray(["apple", "orange", "apple", "banana"])
285+
set->Set.toArray // ["apple", "orange", "banana"]
286+
```
287+
*/
288+
external toArray: t<'a> => array<'a> = "Array.from"

0 commit comments

Comments
 (0)