From: Juan Matias on
glen wrote:
> Just thought I'd post a solution I came up with to finding
> combinations (as in, permutations and combinations) of arrays. For
> example, combining:
>
> [1,2] and [3,4,5]
>
> should return:
>
> [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]]
>
> which is easy enough. But I wanted something that combine several
> arrays at once, ie:
>
> [[1,2],[3,4,5],[6,7]].combine
> => [[1, 3, 6], [1, 3, 7], [1, 4, 6], [1, 4, 7], [1, 5, 6], [1, 5, 7],
> [2, 3, 6], [2, 3, 7], [2, 4, 6], [2, 4, 7], [2, 5, 6], [2, 5, 7]]
>

And why not something like:

[1,2].combine([3,4]).combine([5,6,7])

I do that with this code:

class Array
def combine(otherArray)
aux = []
self.each do |self_elem|
otherArray.each do |other_elem|
aux << [self_elem,other_elem]
end
end
aux.map {|elem| elem.flatten }
end
end

Juan Matias
--
Posted via http://www.ruby-forum.com/.

From: Phrogz on
On Jun 7, 9:52 pm, Juan Matias <jmrepe...(a)gmail.com> wrote:
> glen wrote:
> > Just thought I'd post a solution I came up with to finding
> > combinations (as in, permutations and combinations) of arrays. For
> > example, combining:

Juan, it would appear that glen posted this question/tip 3.5 years
ago. Any reason you were responding to it now? (Which, in turn, lured
me into responding. :)

From: Juan Matias on
Gavin Kistner wrote:
> On Jun 7, 9:52�pm, Juan Matias <jmrepe...(a)gmail.com> wrote:
>> glen wrote:
>> > Just thought I'd post a solution I came up with to finding
>> > combinations (as in, permutations and combinations) of arrays. For
>> > example, combining:
>
> Juan, it would appear that glen posted this question/tip 3.5 years
> ago. Any reason you were responding to it now? (Which, in turn, lured
> me into responding. :)

Sure Gavin, I'm looking for something like that and found this post.
Maybe result useful to someone. You have another solution for this?
Thanks
--
Posted via http://www.ruby-forum.com/.

From: Kamal Ahmed on
The examples seem to be missing
If there is code snippets, it would help.
-Kamal.




________________________________
From: Juan Matias <jmrepetti(a)gmail.com>
To: ruby-talk ML <ruby-talk(a)ruby-lang.org>
Sent: Tue, June 8, 2010 12:33:24 AM
Subject: Re: array and hash combine methods

Gavin Kistner wrote:
> On Jun 7, 9:52�pm, Juan Matias <jmrepe...(a)gmail.com> wrote:
>> glen wrote:
>> > Just thought I'd post a solution I came up with to finding
>> > combinations (as in, permutations and combinations) of arrays. For
>> > example, combining:
>
> Juan, it would appear that glen posted this question/tip 3.5 years
> ago. Any reason you were responding to it now? (Which, in turn, lured
> me into responding. :)

Sure Gavin, I'm looking for something like that and found this post.
Maybe result useful to someone. You have another solution for this?
Thanks
--
Posted via http://www.ruby-forum.com/.
From: Robert Klemme on
2010/6/8 Juan Matias <jmrepetti(a)gmail.com>:
> glen wrote:
>> Just thought I'd post a solution I came up with to finding
>> combinations (as in, permutations and combinations) of arrays. For
>> example, combining:
>>
>> [1,2] and [3,4,5]
>>
>> should return:
>>
>> [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]]
>>
>> which is easy enough. But I wanted something that combine several
>> arrays at once, ie:
>>
>> [[1,2],[3,4,5],[6,7]].combine
>> => [[1, 3, 6], [1, 3, 7], [1, 4, 6], [1, 4, 7], [1, 5, 6], [1, 5, 7],
>> [2, 3, 6], [2, 3, 7], [2, 4, 6], [2, 4, 7], [2, 5, 6], [2, 5, 7]]
>>
>
> And why not something like:
>
>  [1,2].combine([3,4]).combine([5,6,7])
>
> I do that with this code:
>
> class Array
>  def combine(otherArray)
>    aux = []
>    self.each do |self_elem|
>      otherArray.each do |other_elem|
>        aux << [self_elem,other_elem]
>      end
>    end
>    aux.map {|elem| elem.flatten }
>  end
> end

I'd rather do this:

module Enumerable
def combine(enum)
if block_given?
each do |*a|
enum.each do |*b|
yield *a, *b
end
end
self
else
enum_for(:combine, enum)
end
end
end

[1,2].combine([3,4]) do |*a|
p a
end

puts "--------------"

[1,2].combine([3,4]).each do |*a|
p a
end

puts "--------------"

[1,2].combine([3,4]).combine([5,6]) do |*a|
p a
end

puts "--------------"

[1,2].combine([3,4]).combine([5,6]).each do |*a|
p a
end

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/