Array product with nested arrays

I have a requirement that i need to not get the full cartesian product
but a unique ordered single product of an array with nested arrays. For
example.

[[“X”,“Y”] , [“E”], [“S”,“T”,“R”] ]

My requirement is that the function would generate

[“X”, “E”, “S”]
[“X”, “E”, “T”]
[“X”, “E”, “R”]
[“Y”, “E”, “S”]
[“Y”, “E”, “T”]
[“Y”, “E”, “R”]

I started writing a function with recursion to do it but i though there
must be a nicer way. If anyone had suggestions on a nice way to get the
results that would be great.
Thanks.

Hi,

How is that different from the cartesion product of all subarrays?

a = [[“X”, “Y”], [“E”], [“S”, “T”, “R”]]
prod = a[0].product *a[1…-1]
p prod

Jan E. wrote in post #1052440:

Hi,

How is that different from the cartesion product of all subarrays?

a = [[“X”, “Y”], [“E”], [“S”, “T”, “R”]]
prod = a[0].product *a[1…-1]
p prod

Ok maybe i didn’t fully understand how product works as i though it
would give me too many result combinations but that looks like what i am
looking for so thanks very much.
Cheers.
Barry.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs