On 10/10/07, Robert Dober <robert.dober / gmail.com> wrote:

> > ----
> Oops I just made a fool out of myself in a different thread, funny
> nobody hollered at me so far, please let me repair the damage here
>  a3 = a1.select { |e| a2.include?(e) }
> is *not* a1 & a2
> [1,1,2].select{ |x| [1,1,3].include? x }
> is *not*
> [1,1,2] & [1,1,3]


The difference being that & eliminates duplicates from the result.

Another semantic equivalent for a1.select{ |e| a2.include?(e) }

is either
a1 - (a1 - a2)
or
a2 - (a2 - a1)

Whether or not one of these performs better than the select depends on
the 'statistical' properites of the arrays:

require "benchmark"

TESTS = 10_000
a1 = [1, 2, 3, 1, 1, 4, 5] * 100
a2 = [1, 3, 5]

r1 = a1.select { |e| a2.include?(e) }
r2 = a2.select { |e| a1.include?(e) }
r3 = a1 - (a1 - a2)
r4 = a2 - (a2 - a1)

puts r1 = r2 ? "Good" : "Bad"
puts r2 = r3 ? "Good" : "Bad"
puts r3 = r4 ? "Good" : "Bad"


Benchmark.bmbm do |results|
  results.report("select a1 in a2:") { TESTS.times { a1.select { |e|
a2.include?(e) } } }
  results.report("select a2 in a1:") { TESTS.times { a2.select { |e|
a1.include?(e) } } }
  results.report("a1 - (a1 - a2) :") { TESTS.times { a1 - (a1 - a2 ) } }
  results.report("a2 - (a2 - a1) :") { TESTS.times { a2 - (a2 - a1) } }
end

Produces:

Good
Good
Good
Rehearsal ----------------------------------------------------
select a1 in a2:   3.360000   0.020000   3.380000 (  3.417788)
select a2 in a1:   0.030000   0.000000   0.030000 (  0.026397)
a1 - (a1 - a2) :   0.670000   0.000000   0.670000 (  0.680342)
a2 - (a2 - a1) :   0.240000   0.010000   0.250000 (  0.250795)
------------------------------------------- total: 4.330000sec

                       user     system      total        real
select a1 in a2:   3.360000   0.010000   3.370000 (  3.386324)
select a2 in a1:   0.020000   0.000000   0.020000 (  0.024740)
a1 - (a1 - a2) :   0.660000   0.000000   0.660000 (  0.670034)
a2 - (a2 - a1) :   0.240000   0.000000   0.240000 (  0.242316)


-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/