20/01/12 17:33:54.06 kqg5PnqA.net
>>601 Ruby
def bs(ary, &cond)
return ary[0] && cond.call(ary[0]) ? ary[0] : ary[1] && cond.call(ary[1]) ? ary[1] : nil if ary.size < 3
mid = ary.size / 2
bs(ary[cond.call(ary[mid]) ? 0..mid : mid + 1..-1], &cond)
end
p bs([1,3,5,7,9]){|i| i > 0} # => 1
p bs([1,3,5,7,9]){|i| i > 3} # => 5
p bs([1,3,5,7,9]){|i| i > 9} # => nil