Skip to content

Commit 1ac578f

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 1ac578f

File tree

3 files changed

+114
-0
lines changed

3 files changed

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

+102
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,105 @@ 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+
@send
250+
external isSubsetOf: (t<'a>, t<'a>) => bool = "isSubsetOf"
251+
252+
/**
253+
Returns a bool indicating if the all values in the given set are in the set.
254+
255+
## Examples
256+
```rescript
257+
let set1 = Set.fromArray(["apple", "banana", "pear"])
258+
let set2 = Set.fromArray(["apple", "banana"])
259+
set1->Set.isSupersetOf(set2) // true
260+
```
261+
*/
262+
@send
263+
external isSupersetOf: (t<'a>, t<'a>) => bool = "isSupersetOf"
264+
265+
/**
266+
Returns a new set with the values of the set that are in both the set and the other set.
267+
268+
## Examples
269+
```rescript
270+
let set1 = Set.fromArray(["apple", "orange", "banana"])
271+
let set2 = Set.fromArray(["apple", "banana", "pear"])
272+
set1->Set.union(set2) // Set.fromArray(["apple", "orange", "banana", "pear"])
273+
```
274+
*/
275+
@send
276+
external union: (t<'a>, t<'a>) => t<'a> = "union"
277+
278+
/**
279+
`toArray(set)` returns an array of all values of the set.
280+
281+
## Examples
282+
```rescript
283+
let set = Set.fromArray(["apple", "orange", "apple", "banana"])
284+
set->Set.toArray // ["apple", "orange", "banana"]
285+
```
286+
*/
287+
external toArray: t<'a> => array<'a> = "Array.from"

0 commit comments

Comments
 (0)