It's surprising how easily and quickly Java code can be turned into Ruby code.
class Array
def bsearch ( target )
high = self.length
low = -1
while high - low > 1
probe = (high + low) / 2
if self[probe] > target
high = probe
else
low = probe
end
end
if low == -1 || self[low] != target
return -1
else
return low
end
end
def bsearch_dups ( target )
high = self.length
low = -1
while high - low > 1
probe = (high + low) / 2
if self[probe] < target
low = probe
else
high = probe
end
end
if high == self.length || self[high] != target
return -1
else
return high
end
end
def range ( floor, ceiling )
answer = []
# work on floor
high = self.length
low = -1
while high - low > 1
probe = (high + low) / 2
if self[probe] < floor
low = probe
else
high = probe
end
end
answer[0] = low
# work on ceiling
high = self.length
low = -1
while high - low > 1
probe = (high + low) / 2
if self[probe] > ceiling
high = probe
else
low = probe
end
end
answer[1] = high
return answer
end
end
No doubt it could be written more clearly, but even with the basic translation it's nicer than the Java version. For a start, the routines are now methods on Arrays. Damn sensible place to keep them, although I'm sure several of the 4 readers of this post will disagree (including myself depending on the phase of the moon).
Secondly, it's type free.[1] This is a good thing.
Thirdly, think about this code in Perl. What would be written differently? Would you be tempted to have another parameter, a comparator? What would you do about the difference between comparing strings and numbers?
Enough of this for now.
[1] I'm sure there's a better, or more correct, term ('independent', 'agnostic', something), but I don't know it. Feel free to comment it.
Re:Changing the core
koschei on 2003-03-24T02:46:21
It all goes to hell =)
Generally, if you're modifying a class that isn't yours then you
should only be adding stuff that's really generic. For example,
a binary search to Array.
Otherwise, you should be making your own Array subclass.
I wouldn't be surprised if someone's already implemented
a bsearch (in fact, I think Hal Fulton has one in "The Ruby Way").
Ideally someone would be collecting these useful generics into
a package, somewhere. Think List::Util =)
But, yeah, it can go to hell.