Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Last active June 10, 2017 17:35
Show Gist options
  • Save michaelficarra/8a74c614ccc6f93d8b76be64faf57484 to your computer and use it in GitHub Desktop.
Save michaelficarra/8a74c614ccc6f93d8b76be64faf57484 to your computer and use it in GitHub Desktop.
Set using hashCode via ECMAScript interfaces proposal
// 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