Cartesian product of two or more lists: Difference between revisions

→‎{{header|Groovy}}: Changed to a "more natural" multiply operator overload
(→‎{{header|Groovy}}: Initial solution)
(→‎{{header|Groovy}}: Changed to a "more natural" multiply operator overload)
Line 844:
 
=={{header|Groovy}}==
'''Solution:'''<br>
The following ''CartesianCategory'' class allows for modification of regular ''Iterable'' interface behavior, overloading ''Iterable'''s ''multiply'' (*) operator to perform a Cartesian Product when the second operand is also an ''Iterable''.
<lang groovy>def cartProd(Iterable ... lists) {
<lang groovy>class CartesianCategory {
assert lists
static Iterable multiply(Iterable a, Iterable b) {
def order = lists.size()
assert order[a,b].every { it != >null 1}
def dims(m,n) = lists*[a.size(),b.size()]
(0..<(n0m*n1n)).inject([]) { prod, i -> prod << [lists[0]a[i.intdiv(n1n)],lists[1] b[i%n1n]].flatten() }
if (order == 2) {
def (n0,n1) = dims
(0..<(n0*n1)).inject([]) { prod, i -> prod << [lists[0][i.intdiv(n1)],lists[1][i%n1]] }
} else {
def (l1,l2) = lists[0..1]
Iterable[] newLists = [cartProd(l1,l2)] + lists[2..<order]
cartProd(newLists).collect { it.flatten() }
}
}</lang>
'''Test:'''<br>
The ''mixin'' method call is necessary to make the multiply (*) operator work.
<lang groovy>println "[1, 2] × [3, 4] = ${cartProd([1, 2], [3, 4])}"
<lang groovy>Iterable.metaClass.mixin CartesianCategory
println "[3, 4] × [1, 2] = ${cartProd([3, 4], [1, 2])}"
 
println "[1, 2] × [] = ${cartProd([1, 2], [])}"
println "[1, 2] × [13, 24] = ${cartProd([]1, 2] * [13, 24])}"
println "[17763, 1789] × [7, 12] × [4, 14, 23] × [01, 12] = ${cartProd([17763, 17894], [7, 12],* [41, 14, 23], [0, 12])}"
println "[1, 2, 3] × [30] × [500, 100] = ${cartProd([1, 2, 3], [30],* [500, 100])}"
println "[1, 2, 3] × [] × [5001, 1002] = ${cartProd([1, 2, 3], [],* [5001, 1002])}"
println "[1776, 1789] × [7, 12] × [4, 14, 23] × [0, 1] = ${[1776, 1789] * [7, 12] * [4, 14, 23] * [0, 1]}"
println "[31, 42, 3] × [130] × [500, 2100] = ${cartProd([31, 4]2, 3] * [130] * [500, 2100])}"
<lang groovy>println "[1, 2, 3] × [3] × [500, 4100] = ${cartProd([1, 2], [3] * [] * [500, 4100])}"
 
println "[John,Paul,George,Ringo] × [Emerson,Lake,Palmer] × [Simon,Garfunkle] = ["
cartProd( ["John","Paul","George","Ringo"], * ["Emerson","Lake","Palmer"], * ["Simon","Garfunkle"] ).each { println "\t${it}," }
println "]"</lang>
'''Output:'''
Anonymous user