Closed ersimont closed 1 year ago
Perhaps it needs a more professional name 🙂.
export class MagicalMap<Key, Value> { #map = new Map<Key, Value>(); constructor(private createNewValue: () => Value) {} get(key: Key): Value { if (this.#map.has(key)) { return this.#map.get(key)!; } const value = this.createNewValue(); this.#map.set(key, value); return value; } keys(): Iterable<Key> { return this.#map.keys(); } values(): Iterable<Value> { return this.#map.values(); } }
Make createNewValue accept the key, too!
createNewValue
Will be in the next release
Perhaps it needs a more professional name 🙂.