Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active April 5, 2020 21:25
Show Gist options
  • Save slikts/3a064ee7f00b688a7a8b97e40b6dec61 to your computer and use it in GitHub Desktop.
Save slikts/3a064ee7f00b688a7a8b97e40b6dec61 to your computer and use it in GitHub Desktop.
export default class DefaultMap<K, V> extends Map<K, V> {
constructor(private init: (key: K) => V) {
super();
}
get(key: K): V {
if (!this.has(key)) {
const value = this.init(key);
this.set(key, value);
return value;
}
return super.get(key)!;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment