I’m trying to convert this Ruby code to Crystal.
haystack = [0,1,4,5,6,7,8,9,12,26,45,67,78,90,98,123,211,234,456,769,865,2345,3215,14345,24324]
needles = [0,42,45,24324,99999]
needles.select{|needle| haystack.bsearch{|hay| needle <=> hay} } # => [0, 45, 24324]
This code will run in Crystal, but not give the same (desired) result.
According to the Crystal docs for bsearch
https://crystal-lang.org/api/0.28.0/Indexable.html#bsearch(%26block)-instance-method
you can’t use the <=>
operator, and only shows examples using >=
, >
, <
, and <=
.
Does the current implementation of bsearch
allow for searching for equality? and if so can you provide an example for this code example?