Skip to content

Instantly share code, notes, and snippets.

@jraska
Created May 28, 2019 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jraska/24c1ff2b5da3d4ac43ad7507b9189b80 to your computer and use it in GitHub Desktop.
Save jraska/24c1ff2b5da3d4ac43ad7507b9189b80 to your computer and use it in GitHub Desktop.
fun <K, V> lazyMap(initializer: (K) -> V): Map<K, V> {
val map = mutableMapOf<K, V>()
return map.withDefault { key ->
val newValue = initializer(key)
map[key] = newValue
return@withDefault newValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment