Hi –
“Bill Kelly” billk@cts.com wrote in message
news:002901c23e70$402b3d30$fa7b1b42@musicbox…
Hi,
Hm, maybe you want to benchmark my solution, Chr0002, as well?
Whoops, I don’t seem to have Array#any? and Array#all?.
New
methods in 1.7 I guess?
Yup, they are a generalization of && and || to Enumerables.
module Enumerable
def all?
each {|e| return false unless yield e }
return true
end
def any?
each {|e| return true if yield e }
return false
end
end
At first glance I thought that #any? didn’t add anything to just using
the Boolean value of the result of #detect… until I tried this:
ar = [1,2,3,nil,5]
puts “nil found by any?” if ar.any? {|e| e == nil }
puts “nil found by detect” if ar.detect {|e| e == nil}

David
···
On Thu, 8 Aug 2002, Christoph wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
“Bill Kelly” billk@cts.com wrote in
…
The second failed on just the latter dataset, above. (And both
failed on my larger ‘benchmark’ dataset…)
Sorry, for my editing mistakes - I’ll (hopefully) corrected them.
/Christoph
…
Yup, they are a generalization of && and || to Enumerables.
module Enumerable
def all?
each {|e| return false unless yield e }
return true
end
def any?
each {|e| return true if yield e }
return false
end
end
At first glance I thought that #any? didn’t add anything to just using
the Boolean value of the result of #detect… until I tried this:
Hm, the #detect method was new to me;-)
ar = [1,2,3,nil,5]
puts “nil found by any?” if ar.any? {|e| e == nil }
puts “nil found by detect” if ar.detect {|e| e == nil}
In some sense
module Enumerable
def all?
last = true
each {|e| return e unless last = yield e }
return last
end
def any?
last = nil # or false?
each { |e| return e if last = yield e }
return last
end
end
might have been a better (but sort of useless;-) generalization
of Ruby’s shortcut logic for && and ||.
/Christoph
···
dblack@candle.superlink.net wrote
…
module Enumerable
def all?
last = true
each {|e| return e unless last = yield e }
return last
end
def any?
last = nil # or false?
each { |e| return e if last = yield e }
return last
end
end
Sigh …
module Enumerable
def all?
last = true
each {|e| return last unless last = yield e }
return last
end
def any?
last = nil # or false?
each { |e| return last if last = yield e }
return last
end
end
/Christoph
···
“Christoph” chr_news@gmx.net wrote
“Bill Kelly” billk@cts.com wrote in
…
The second failed on just the latter dataset, above. (And both
failed on my larger ‘benchmark’ dataset…)
Sorry, for my editing mistakes - I’ll (hopefully) corrected them.
Both solutions still fail on:
items = [ 1,2,2 ]
rows = [ [1,2], [1,2], [1,3] ]
Note, this isn’t one of the unit tests on the wiki page - I’ll add
it (there’s a similar one, test_true_5, but, your algorithms pass
that test but not this one above…) . . . [added as test_true_7]
Regards,
Bill
···
From: “Christoph” chr_news@gmx.net
“Bill Kelly” billk@cts.com wrote in
…
Both solutions still fail on:
items = [ 1,2,2 ]
rows = [ [1,2], [1,2], [1,3] ]
Note, this isn’t one of the unit tests on the wiki page - I’ll add
it (there’s a similar one, test_true_5, but, your algorithms pass
that test but not this one above…) . . . [added as test_true_7]
Hm, these units tests are moving targets;-). I changed the
algorithm to cover this test case. I also fixed editing
mistake in the Unit tests …
/Christoph
[…]
Hm, these units tests are moving targets;-). I changed the
algorithm to cover this test case. I also fixed editing
mistake in the Unit tests …
Congrats! It passes all the tests… for now!!! 
Here’s how it performed relative to a couple others from
before that still pass all the tests:
chr (10 iterations) 3.204 (0.3204 per iter) nitems=60
george (10 iterations) 40.638 (4.0638 per iter) nitems=60
bwk (10 iterations) 78.503 (7.8503 per iter) nitems=60
Interestingly, though, although an order of magnitude faster
than those competitors, it seems to have the same O() somehow?
Because with nitems=70 george and bwk took “forever”. With yours
at 70 we have:
chr (10 iterations) 19.288 (1.9288 per iter) nitems=70
But with nitems=80, yours also took “forever”. (I broke it
after about a half hour still not finishing the 1st iteration.)
I was kind of surprised by that . . .
Regards,
Bill
···
From: “Christoph” chr_news@gmx.net