How can I get a list of a? ```data class Item(val a: Int, val b: Int) val items = listOf(Item(1,2),...
a
How can I get a list of a?
Copy code
data class Item(val a: Int, val b: Int)

val items = listOf(Item(1,2), Item(3,4))

val aList = // [1,3]
j
Copy code
val aList = items.map { it.a }
1
👍 1
a
How can I do the inverse? Construct items from aList and bList in a neat way?
j
aList zip bList will give you list of pairs, you can also provide a transform function
Copy code
aList.zip(bList) { (a, b) ->
    Item(a, b)
}
a
zip works, but couldn't provide a transform function: ERROR: Iterable<TypeVariable(T)>.zip(Array<out TypeVariable(R)>, (a: TypeVariable(T), b: TypeVariable(R)) → TypeVariable(V)) where T = TypeVariable(T), R = TypeVariable(R), V = TypeVariable(V) for inline fun <T, R, V> Iterable<T>.zip(other: Array<out R>, transform: (a: T, b: R) → V): List<V> defined in kotlin.collections Iterable<TypeVariable(T)>.zip(Iterable<TypeVariable(R)>, (a: TypeVariable(T), b: TypeVariable(R)) → TypeVariable(V)) where T = TypeVariable(T), R = TypeVariable(R), V = TypeVariable(V) for inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (a: T, b: R) → V): List<V> defined in kotlin.collections
So, I guess I will do this:
Copy code
aList.zip(bList).map { Item(it.first, it.second) }
just remove the parenthisis over (a, b) and it will be fine
1