-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.js
63 lines (53 loc) · 1.56 KB
/
set.js
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
/**
* Create an empty Set
* O(1)
* @returns {function(*): boolean}
*/
export const empty = ()=>(element)=>false;
/**
* Create a Set with one element
* O(1)
* @param element {*}
* @returns {function(*): boolean}
*/
export const singletonSet = (element)=>(anotherElement)=>element==anotherElement;
/**
* Check wether an element is inside a Set
* O(n)
* @param set {function(*): boolean}
* @param element {*}
* @returns boolean
*/
export const contains = (set,element)=> set(element);
/**
* Create the union of two Sets (A ∪ B)
* O(1)
* @param setA {function(*): boolean}
* @param setB {function(*): boolean}
* @returns {function(*): boolean}
*/
export const union = (setA,setB) => (element)=>contains(setA,element) || contains(setB,element);
/**
* Create the intersection of two Sets (A ∩ B)
* O(1)
* @param setA {function(*): boolean}
* @param setB {function(*): boolean}
* @returns {function(*): boolean}
*/
export const intersect = (setA,setB)=> (element)=>contains(setA,element) && contains(setB,element);
/**
* Create the difference of two sets (A / B)
* O(1)
* @param setA {function(*): boolean}
* @param setB {function(*): boolean}
* @returns {function(*): boolean}
*/
export const diff = (setA,setB)=> (element)=>contains(setA,element) && !contains(setB,element);
/**
* Return a Set of Elements that Match condition
* O(1)
* @param set {function(*): boolean}
* @param condition {function(*): boolean}
* @returns {function(*): boolean}
*/
export const filter = (set,condition) => (element)=>condition(element) && contains(set,element);