How would I best go about producing multiple lists...
# announcements
d
How would I best go about producing multiple lists from a single one? Right now I am doing two
map
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:
Copy code
val list = listOf(1, 2, 3)
val a = list.map { expensiveOperation(it).value1 }
val b = list.map { expensiveOperation(it).value2 }
d
Chunked... does something completely different, no?
g
Yes, sorry Why can't you store result of the expensive operation? Like this:
Copy code
class 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 })
}
d
Yes, I was looking for a way to do that in one go...