Last active
March 12, 2019 05:49
-
-
Save pertrai1/ca503e793af9bfa19514e1de08d12c54 to your computer and use it in GitHub Desktop.
Set, Map, WeakSet, WeakMap
This file contains 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
let union = ( set1, set2 ) => { | |
return new Set([...set1, ...set2]); | |
} | |
let first = [1, 3, 1, 5, 7, 3, 9, 12, 15]; | |
let second = [1, 20, 18, 5, 10, 3, 16]; | |
union(first, second); /*? */ | |
let intersection = (set1, set2) => { | |
return new Set( | |
[...set1].filter(elem => set2.has(elem)) | |
) | |
} | |
let set1 = new Set([1, 2, 3, 4, 5]); | |
set1.has(3); /*? */ | |
let set2 = new Set([1, 3, 5, 7, 9]); | |
set2.has(3); /*? */ | |
intersection(set1, set2); /*? */ | |
let intersect = new Set([1, 3, 5, 6, 3, 10]); | |
let intersect2 = new Set([2, 6, 3, 15, 7]); | |
intersection(intersect, intersect2); /*? */ |
This file contains 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
const myArray = [1, 4, 1, 3, 8, 4, 9, 12, 15, 8, 17, 20]; | |
function removeDuplicates(arr) { | |
return [ ...new Set(arr) ]; | |
} | |
removeDuplicates(myArray); |
This file contains 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
let union = ( set1, set2 ) => { | |
return new Set([...set1, ...set2]); | |
} | |
let first = [1, 3, 1, 5, 7, 3, 9, 12, 15]; | |
let second = [1, 20, 18, 5, 10, 3, 16]; | |
union(first, second); /*? */ | |
let intersection = (set1, set2) => { | |
return new Set( | |
[...set1].filter(elem => set2.has(elem)) | |
) | |
} | |
let set1 = new Set([1, 2, 3, 4, 5]); | |
set1.has(3); /*? */ | |
let set2 = new Set([1, 3, 5, 7, 9]); | |
set2.has(3); /*? */ | |
intersection(set1, set2); /*? */ | |
let intersect = new Set([1, 3, 5, 6, 3, 10]); | |
let intersect2 = new Set([2, 6, 3, 15, 7]); | |
intersection(intersect, intersect2); /*? */ | |
let difference = (set1, set2) => { | |
return new Set( | |
[...set1].filter(elem => !set2.has(elem)) | |
) | |
} | |
difference(intersect, intersect2); /*? */ |
This file contains 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
let _width = new WeakMap(); | |
class Square { | |
constructor( width ) { | |
_width.set( this, width ); | |
} | |
get area() { | |
let width = _width.get( this ); | |
return width * width; | |
} | |
get removeArea() { | |
return _width.delete(this); | |
} | |
} | |
let mySquare = new Square(10); /*? */ | |
mySquare.area; /*? */ | |
mySquare.delete; /*? */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment