Maybe there should be `associateWithNotNull { }` f...
# stdlib
d
Maybe there should be
associateWithNotNull { }
for when the value is resolved to be
null
, just like
mapNotNull { }
?
Something like:
Copy code
inline fun <K, V> Iterable<K>.associateWithNotNull(valueSelector: (K) -> V?): Map<K, V> {
	val result = LinkedHashMap<K, V>(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))
	return associateWithToNotNull(result, valueSelector)
}

inline fun <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithToNotNull(destination: M, valueSelector: (K) -> V?): M {
	for (element in this) {
		val value = valueSelector(element)
		if (value != null) destination.put(element, value)
	}
	return destination
}
It would be useful for when part of the input shouldn't be in the map (like invalid/empty values)