diesieben07
08/22/2018, 9:31 PMmap
operations after another, but that requires me to do some computations twice. Do I really have to write a for loop and add to two lists?
Example:
val list = listOf(1, 2, 3)
val a = list.map { expensiveOperation(it).value1 }
val b = list.map { expensiveOperation(it).value2 }
gcx11
08/22/2018, 9:35 PMdiesieben07
08/22/2018, 9:36 PMgcx11
08/22/2018, 10:11 PMclass Foo(val value1: Int, val value2: Int)
fun foo(value: Int): Foo {
return Foo(value, value * 2)
}
fun main(args: Array<String>) {
val l = listOf(1, 2, 3)
val newList = l.map { foo(it) }
val a = newList.map { it.value1 })
val b = newList.map { it.value2 })
}
diesieben07
08/22/2018, 10:15 PM