fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

Array#find & Array#bsearch are not interchangeable #192

Closed alejandrobabio closed 3 years ago

alejandrobabio commented 3 years ago

A simple example with a sorted array:

irb(main):002:0> ary = [0, 4, 7, 10, 12]
=> [0, 4, 7, 10, 12]
irb(main):012:0> ary.bsearch {|e| e == 10}
=> nil
irb(main):013:0> ary.find {|e| e == 10}
=> 10
delucas commented 3 years ago
irb(main):002:0> ary = [0, 4, 7, 10, 12]
=> [0, 4, 7, 10, 12]
irb(main):012:0> ary.bsearch {|e| 10 - e}
=> 10
irb(main):013:0> ary.find {|e| e == 10}
=> 10
alejandrobabio commented 3 years ago

After reading the Array#bsearch docs I understood that it doesn't work with an equal condition, and the block could need to be rewritten to match the bsearch requirements. So, I close this issue.