Created
May 17, 2019 19:18
-
-
Save jarhoads/beef1d8ffe4ca9748aa4e6f7afaf35d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sets | |
let exampleSet = new Set(); | |
// has one property - size | |
exampleSet.add(1); // exampleSet: Set {1} | |
exampleSet.add(1); // exampleSet: Set {1} | |
exampleSet.add(2); // exampleSet: Set {1, 2} | |
// deletion | |
exampleSet.delete(1); // true, exampleSet: Set {2} | |
// contains | |
exampleSet.add(1); | |
exampleSet.has(1); // true | |
exampleSet.has(2); // true | |
exampleSet.has(3); // false | |
let intersects = (A, B) => new Set(Array.from(A).filter(a => B.has(a))); | |
let union = (A, B) => new Set(Array.from(A).concat(Array.from(B))); | |
// performs A - B | |
let difference = (A, B) => new Set(Array.from(A).filter(a => !(B.has(a)))); | |
let setA = new Set([1, 2, 3, 4]); | |
let setB = new Set([2, 3]); | |
console.log(intersects(setA, setB)); | |
let setC = new Set([5, 6]); | |
console.log(union(setA, setC)); | |
console.log(difference(setA, setB)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment