Paul Woitaschek
08/27/2019, 6:56 AMenum class BaseNutrient
-> Map<BaseNutrient, Double>
. Is there a more elegant way than using Basenutrient.values().associate { baseNutrient -> baseNutrient to 0.0 }
As I need that so frequently it feels like I’m overseeing sth in the stdlibPavlo Liapota
08/27/2019, 7:11 AMassociateWith
function where you need to return just a value instead of a pair.
So you can write list.associateWith { 0.0 }
.
But this function is defined for Iterable
only. So I usually define my own extension function:
inline fun <K, V> Array<out K>.associateWith(valueSelector: (K) -> V): Map<K, V> {
return asIterable().associateWith(valueSelector)
}
Amirul Zin
08/27/2019, 7:12 AMenum class BaseNutrient(val value:Double){
NOODLE(100.0),
CARROT(50.0),
}
Pavlo Liapota
08/27/2019, 7:16 AMMyEnum.values().associateWith { foo(it) }
But if it is constant, then yes, property is the way to go.Paul Woitaschek
08/27/2019, 7:18 AMPaul Woitaschek
08/27/2019, 7:20 AMdarkmoon_uk
08/28/2019, 7:09 AMenum class Nutrient(val value:(Nutrient)->Double){
APPLE(myNutrientFunctionFruit),
CARROT(myNutrientFunctionVeg),
SWEDE(myNutrientFunctionVeg);
val kiloJoules : Double get() = this.value()
}