Last active
May 9, 2016 10:39
-
-
Save heinrisch/d6ceac8fb373a880496c4a7a2e1cf8ad to your computer and use it in GitHub Desktop.
Easy lookup creation from lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.* | |
// Already in Kotlin std lib: list.mapTo(mutableSetOf(), keyGenerator) | |
fun <K, V> List<V>.createLookupSet(keyGenerator: (V) -> K) : HashSet<K> { | |
return fold(HashSet<K>(), {set, value -> set.add(keyGenerator(value)); set}) | |
} | |
// Already in Kotlin std lib: list.associateBy(keyGenerator) | |
fun <K, V> List<V>.createLookupMap(keyGenerator: (V) -> K) : HashMap<K, V> { | |
return fold(HashMap<K, V>(), {map, value -> map.put(keyGenerator(value), value); map}) | |
} | |
// Already in Kotlin std lib: list.groupBy(keyGenerator) | |
fun <K, V> List<V>.createLookupMapList(keyGenerator: (V) -> K) : HashMap<K, MutableList<V>> { | |
return DefaultListHashMap<K, V>().createLookup(this, keyGenerator) | |
} | |
// Instead of this, use getOrPut when wanting to insert into HashMap for same functionality | |
// Example: hashMap.getOrPut(key) { mutableListOf() } | |
class DefaultListHashMap<K, V>() : HashMap<K, MutableList<V>>() { | |
fun put(key: K, value: V) { | |
check(key) | |
get(key)!!.add(value) | |
} | |
fun check(key: K) { | |
if(!containsKey(key)){ | |
put(key, mutableListOf()) | |
} | |
} | |
override fun get(key: K): MutableList<V> { | |
check(key) | |
return super.get(key)!! | |
} | |
fun createLookup(values : List<V>, keyGenerator: (V) -> K) : DefaultListHashMap<K, V> { | |
values.forEach { put(keyGenerator(it), it) } | |
return this | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SSCCE (Short, Self Contained, Correct Example) for this:
Update: Example for
MutableMap<K, V>.getOrPut()
which is like Python'sdict.setdefault()
ordefaultdict
, but additionally inlined and using a lazy default value.