Last active
August 29, 2015 13:56
-
-
Save gdevanla/8868974 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Cartesian product of input (for Integers) | |
//similar to itertools.product in Python | |
def l = [ [1,2] , [3,4] , [5, 6]] | |
def product(l){ | |
def p = { l1, l2 -> | |
l1.inject([]) { acc, i -> | |
acc + l2.inject([]) { acc1, j -> | |
if ( i instanceof List) { acc1 << i + j} | |
else { acc1 << [i, j]}}}} | |
l.inject() { acc, it -> p(acc, it) } | |
} | |
println product(l) | |
//[[1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment