class Range
alias orig_max max
def max
if ( self.begin.kind_of?(Integer) &&
self.end.kind_of?(Integer) &&
( self.exclude_end? ?
self.begin < self.end :
self.begin <= self.end ) )
self.exclude_end? ? self.end - 1 : self.end
else
orig_max
end
end
end
If i try this
puts (1…10).max
it runs fine.
If i try this
puts (1…100000000).max
It is extremely slow. Instead of using straight math max= 100000000-1
it uses some king of interator to find out the value of max.
What is the catch here?
On Oct 12, 2007, at 8:05 PM, Brian A. wrote:
you code does not return the max, it returns the ‘end’, and
incorrectly in some cases.
Fixed now
any range/interval whose end > start must have size > 0. therefore
the max cannot == start. i think you are confusing the concept of
sequence with that of a range
Range#max is mixed in from Enumerable
“When used as an iterator, ranges return each value in the sequence.” Pickaxe p. 597
On Oct 12, 2007, at 8:05 PM, Brian A. wrote:
you code does not return the max, it returns the ‘end’, and
incorrectly in some cases.
Fixed now
any range/interval whose end > start must have size > 0. therefore
the max cannot == start. i think you are confusing the concept of
sequence with that of a range
Range#max is mixed in from Enumerable
“When used as an iterator, ranges return each value in the sequence.” Pickaxe p. 597
An elephant’s trunk feels different than its tail
“range max conversation”.max == this post
File Not Found
June 08, 2006
The file you were looking for could not be found.
Attempted URL: /faq/rubyfaq.html
It is possible that you typed the URL incorrectly or that you clicked on
a bad link.
If i try this
puts (1…10).max
it runs fine.
If i try this
puts (1…100000000).max
It is extremely slow. Instead of using straight math max= 100000000-1
it uses some king of interator to find out the value of max.
What is the catch here?
the OP’s question is about why the current max is slow. the reason
is because ranges are not always sequences.
regards.
Dude, you totally missed your cue about point #3. You were supposed to
say, “no, you’re wrong, an elephant’s trunk feels the same as its
tail”. Cool, I guess we do agree on something
any object can be used in a range in ruby. it
only must respond to #succ.
Actually as discussed not long ago, this is actually too strong a
pre-condition for an object to be used in a range. It’s true that you
need succ if you want to enumerate the range, but for ranges used for,
say an inclusion test, the start and end values only need to be
comparable.
I.e. (2.5…3.14) is a perfectly valid range for such purposes.
Are you complaining about my version of max or the built in? I can’t
tell because the results are identical
not complaining about anything. over the years people have posted
various questions and complaints about ranges: they should do this
and that, etc. most/all of the posts though are based on a
misconception of what ranges are: a very lightweight set of endpoints
with minimal constraints on the contents and, of course, this is what
makes then so generally useful in ruby.
basically i feel that the ruby range is maligned and i’m making sure
the limitations don’t convince people that they aren’t powerful. the
OP, for those who haven’t lost track, had complained about the
performance of range.max. the thing for people still on thread to
remember is simply that the impl is naive because ranges are so
general and that a faster implementation would limit the usefulness
of range itself, as this thread as beat to death.