prog8/compiler/test/helpers/mapCombinations.kt

32 lines
941 B
Kotlin
Raw Normal View History

package prog8tests.helpers
2022-06-26 16:51:03 +00:00
fun <T, U> cartesianProduct(c1: Collection<T>, c2: Collection<U>): Sequence<Pair<T, U>> {
return c1.flatMap { lhsElem -> c2.map { rhsElem -> lhsElem to rhsElem } }.asSequence()
}
2022-06-26 16:51:03 +00:00
fun <T, U, V> cartesianProduct(c1: Collection<T>, c2: Collection<U>, c3: Collection<V>): Sequence<Triple<T, U, V>> {
return sequence {
for (a in c1)
for (b in c2)
for (c in c3)
yield(Triple(a, b, c))
}
2022-06-26 16:51:03 +00:00
}
2022-06-26 16:51:03 +00:00
data class Product<out T, out U, out V, out W>(val first: T, val second: U, val third: V, val fourth: W)
2022-06-26 16:51:03 +00:00
fun <T, U, V, W> cartesianProduct(
c1: Collection<T>,
c2: Collection<U>,
c3: Collection<V>,
c4: Collection<W>
): Sequence<Product<T, U, V, W>> {
return sequence {
for (a in c1)
for (b in c2)
for (c in c3)
for (d in c4)
yield(Product(a, b, c, d))
}
2022-06-26 16:51:03 +00:00
}