Alexander
01/18/2024, 9:39 AMpublic fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
val expectedSize = collectionSizeOrDefault(10)
val listT = ArrayList<T>(expectedSize)
val listR = ArrayList<R>(expectedSize)
for (pair in this) {
listT.add(pair.first)
listR.add(pair.second)
}
return listT to listR
}
Arrow method:
public fun <A, B> Iterable<Pair<A, B>>.unzip(): Pair<List<A>, List<B>> =
fold(emptyList<A>() to emptyList()) { (l, r), x ->
l + x.first to r + x.second
}
To my eyes, this looks worse than what's already built-in because it's using immutable lists in every step, which means it will be creating two new lists for each iteration? While the built-in uses mutable lists internally, and just appends to those lists in each step of the itteration.Alejandro Serrano.Mena
01/18/2024, 11:23 AMsimon.vergauwen
01/18/2024, 1:52 PMsimon.vergauwen
01/18/2024, 1:53 PMAlejandro Serrano.Mena
01/18/2024, 1:53 PMAlexander
01/18/2024, 1:54 PM