jw
03/08/2016, 4:26 PMeshioji
03/08/2016, 4:26 PMjw
03/08/2016, 4:27 PMinline fun <reified K : Enum<K>, V> enumMapOf(vararg pairs: Pair<K, V>)
= EnumMap<K, V>(K::class.java).let { map ->
pairs.forEach { map.put(it.first, it.second) }
}
jw
03/08/2016, 4:27 PMfold
as welljw
03/08/2016, 4:34 PMjw
03/08/2016, 4:35 PMeshioji
03/08/2016, 4:35 PMeshioji
03/08/2016, 4:40 PMeshioji
03/08/2016, 4:40 PMeshioji
03/08/2016, 4:40 PMkirillrakhman
03/08/2016, 4:57 PMUnit
jw
03/08/2016, 4:57 PMkirillrakhman
03/08/2016, 4:57 PMinline fun <reified K : Enum<K>, V> enumMapOf(vararg pairs: Pair<K, V>) = EnumMap<K, V>(K::class.java).apply {
pairs.forEach { put(it.first, it.second) }
}
jw
03/08/2016, 4:59 PMkittinunf
03/08/2016, 5:40 PMassociateByTo
?
inline fun <reified K : Enum<K>, V> enumMapOf(vararg pairs: Pair<K, V>) = pairs.associateByTo(EnumMap<K, V>(K::class.java), { it.first }, { it.second })
yoavst
03/08/2016, 5:55 PMinline fun <reified K : Enum<K>, V> enumMapOf(vararg pairs: Pair<K, V>) = pairs.associateByTo(EnumMap<K, V>(K::class.java), Pair<K, V>::first, Pair<K, V>::second)
It could be nicer if Pair::first
would have worked.eshioji
03/08/2016, 6:43 PMeshioji
03/08/2016, 6:43 PMeshioji
03/08/2016, 6:43 PMenumMapOf
is implemented as followseshioji
03/08/2016, 6:44 PMeshioji
03/08/2016, 6:46 PMeshioji
03/08/2016, 6:46 PMeshioji
03/08/2016, 6:47 PMeshioji
03/08/2016, 6:47 PMeshioji
03/08/2016, 6:50 PMeshioji
03/08/2016, 6:56 PMjw
03/08/2016, 7:12 PMilya.gorbunov
03/09/2016, 2:16 PMcompareTo
represents a total order relation on a set. By redefining compareTo
to partial order you break its contract.voddan
03/09/2016, 3:27 PMvoddan
03/09/2016, 3:28 PMcompareTo
should not throw:
e.compareTo(null) should throw a NullPointerExceptionfrom the docs: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html