Map.getOrPut: treat nulls as missing values.

This commit is contained in:
Ilya Gorbunov
2016-01-22 07:47:54 +03:00
parent a49db730a9
commit 23080f78f7
8 changed files with 21 additions and 44 deletions
@@ -41,6 +41,18 @@ fun <K> Iterable<K>.mapToIndex(): Map<K, Int> {
return map
}
public inline fun <K, V> MutableMap<K, V>.getOrPutNullable(key: K, defaultValue: () -> V): V {
return if (!containsKey(key)) {
val answer = defaultValue()
put(key, answer)
answer
}
else {
get(key) as V
}
}
inline fun <T, C: Collection<T>> C.ifEmpty(body: () -> C): C = if (isEmpty()) body() else this
inline fun <T> Array<out T>.ifEmpty(body: () -> Array<out T>): Array<out T> = if (isEmpty()) body() else this