Last active
June 10, 2017 17:35
-
-
Save michaelficarra/8a74c614ccc6f93d8b76be64faf57484 to your computer and use it in GitHub Desktop.
Set using hashCode via ECMAScript interfaces proposal
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
// https://github.com/michaelficarra/ecmascript-interfaces-proposal | |
interface HasHashCode { | |
hashCode; | |
} | |
class SetUsingHashCode extends Set { | |
constructor(iterable) { | |
super(); | |
this.#map = new Map; | |
for (let x of iterable) { | |
this.add(x); | |
} | |
} | |
add(value) { | |
this.#map.set(value[HasHashCode.hashCode](), value); | |
} | |
// ... others | |
} | |
class Div implements HasHashCode { | |
[HashHashCode.hashCode]() { return this.val; } | |
constructor(val) { this.val = val; } | |
} | |
new SetUsingHashCode().add(new Div(1)).has(new Div(1)) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment