Edoardo Luppi
09/22/2024, 5:10 PMArray<A>
to Array<B>
myArray.mapToArray { ... }
It's strange not having it in the standard library.Joffrey
09/22/2024, 5:11 PMJoffrey
09/22/2024, 5:12 PMEdoardo Luppi
09/22/2024, 5:12 PMJoffrey
09/22/2024, 5:12 PMmap
, right?Edoardo Luppi
09/22/2024, 5:13 PMList<T>
and having to call toTypedArray()
I don't want to allocate that list.Edoardo Luppi
09/22/2024, 5:15 PMval newArray = Array(myArray.size) {
val e = myArray[it]
...
}
Joffrey
09/22/2024, 5:16 PMEdoardo Luppi
09/22/2024, 5:16 PMmapToArray
function wouldn't need to expose the index.Joffrey
09/22/2024, 5:18 PMJoffrey
09/22/2024, 5:18 PMEdoardo Luppi
09/22/2024, 5:19 PMval newArray = Array<NewType>(myArray.size)
myArray.mapTo(newArray) { ... }
But still, two steps instead of one and less code clarity.Edoardo Luppi
09/22/2024, 5:20 PMso you can write your own extension to abstract this awayWill definitely do that. I see I do this in more than 20 places, so extracting is good.
ephemient
09/22/2024, 6:46 PMmapTo
(and other *to
functions) return the target, so it can be done in one stepephemient
09/22/2024, 6:47 PMEdoardo Luppi
09/22/2024, 6:54 PMval newArray = Array<NewType>(myArray.size)
myArray.mapTo(newArray) { ... }
That can't work as Array<NewType>(myArray.size)
requires an initializer for each element.
Also mapTo
doesn't accept arrays.ephemient
09/22/2024, 6:56 PMephemient
09/22/2024, 6:58 PM