Mark
04/05/2021, 4:15 AMinline fun <reified K, V> Map<*, V>.filterKeyIsInstance(): Map<K, V> {
return filterKeyIsInstanceTo(mutableMapOf())
}
inline fun <reified K, V, M : MutableMap<in K, in V>> Map<*, V>.filterKeyIsInstanceTo(destination: M): M {
entries.forEach { (key, value) ->
if (key is K) {
destination[key] = value
}
}
return destination
}
// should this by `out K` or just `K`?
inline fun <K, reified V> Map<out K, *>.filterValueIsInstance(): Map<K, V> {
return filterValueIsInstanceTo(mutableMapOf())
}
inline fun <K, reified V, M : MutableMap<in K, in V>> Map<K, *>.filterValueIsInstanceTo(destination: M): M {
entries.forEach { (key, value) ->
if (value is V) {
destination[key] = value
}
}
return destination
}
inline fun <reified K, reified V> Map<*, *>.filterIsInstance(): Map<K, V> {
return filterIsInstanceTo(mutableMapOf())
}
inline fun <reified K, reified V, M : MutableMap<in K, in V>> Map<*, *>.filterIsInstanceTo(destination: M): M {
entries.forEach { (key, value) ->
if (key is K && value is V) {
destination[key] = value
}
}
return destination
}