Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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