From: Handy Gandy Date: 2010-09-15T02:31:38+09:00 Subject: Re: Iterate over a cartesian product. On Tue, 14 Sep 20 10 17:13:11 +0200, J旦rg W Mittag wrote: > Handy Gandy wrote: >> I have an array of N arrays A_i: [A_1,A_2,...,A_N]. >> >> I want to iterate over the cartesian product A_1xA_2...xA_N. >> >> Is there a simple way of doing this without specifying N? > > ary = [[1, 2], [3, 4, 5], [6, 7, 8, 9]] > ary.first.product(*ary.drop(1)) > # => [ > # => [1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 3, 9], # => [1, 4, 6], > [1, 4, 7], [1, 4, 8], [1, 4, 9], # => [1, 5, 6], [1, 5, 7], [1, 5, > 8], [1, 5, 9], # => [2, 3, 6], [2, 3, 7], [2, 3, 8], [2, 3, 9], # > => [2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 4, 9], # => [2, 5, 6], > [2, 5, 7], [2, 5, 8], [2, 5, 9] # => ] > > jwm That looks so cool I wish I knew what it does.