Skip to content

Instantly share code, notes, and snippets.

@porfirioribeiro
Created January 27, 2023 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porfirioribeiro/5f7b32a38d51e93bafac818eab7b8cc9 to your computer and use it in GitHub Desktop.
Save porfirioribeiro/5f7b32a38d51e93bafac818eab7b8cc9 to your computer and use it in GitHub Desktop.
function getOrSet<K, V>(map: Map<K, V>, key: K, defaultValue: () => V): V {
const value = map.get(key);
if (value == null) {
const answer = defaultValue();
map.set(key, answer);
return answer;
} else {
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment