Created
January 27, 2023 14:16
-
-
Save porfirioribeiro/5f7b32a38d51e93bafac818eab7b8cc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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