Does Kotlin libraries have function to make cartes...
# announcements
u
Does Kotlin libraries have function to make cartesian product? like in python https://docs.python.org/2/library/itertools.html#itertools.product
r
Copy code
list1.flatMap { e1 ->
    list2.map { e2 -> e1 to e2 }
}
s
now we only need a cartProduct extension function that hides this implementation 😂
s
you can make a pr to this repo and I should be releasing it soon (if I can get azure devops to cooperate) https://github.com/snowe2010/kotlin-extensions
u
@Ruckus @Stephan Schroeder what about unknown count of lists? Library implementation should be more universal. Can your implementation works similar like that? https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists#Kotlin
@snowe is your library multiplatform? I'm not interested in Kotlin/JVM
r
@U75957 You could rewrite the code I gave to use a producer, though there are concerns with that. If the producers cannot be iterated multiple times, the inner loop won't work. The easy solution to that would be to just collect the two iterables into lists first. If they are too big, a cartesian product is probably not really what you want anyway...
s
@U75957 I haven't touched multiplatform very much so I'm not sure if it will work. It's just plain kotlin, so I think it should work, but I'm not sure.
149 Views