is it possible to `get` the first of a list of pos...
# announcements
a
is it possible to
get
the first of a list of possible keys from a map -- for example if the keys might not always be exact:
personMap.getFrom("lastname", "LastName", "last_name", "nameLast")
?
m
I don't think there is a function for that in stdlib
Copy code
fun <K,V> Map<K,V>.getFrom(vararg getKeys: K): V? {
    check(getKeys.isNotEmpty()) { "..." }
    val key: K? = getKeys.firstOrNull { it in this.keys }
    return if (key != null) this[key] else null
}
a
thanks... I checked the docs but thought maybe I was missing something. I think something like getKeys.map().filter().first() might be the most efficient
👍 1
even easier:
Copy code
fun <K, V : Any> Map<K, V>.getFrom(vararg potentialKeys: K) : V? {
    return potentialKeys.find { k -> this.containsKey(k) }?.let { this[it] }
}
thanks for the help
m
nice, even better!