Actually, I think the following would be better for those rare people
who find themselves frequently wondering what the largest int in a
range is, and would prefer O(1) instead of O(n):
class Range
alias orig_max max
def max
if ( self.begin.kind_of?(Integer) &&
self.end.kind_of?(Integer) &&
self.begin <= self.end )
self.exclude_end? ? self.end - 1 : self.end
else
orig_max
end
end
end
Now if someone can just come up with a real world example (i.e.
currently existing code in production) of Ruby code that actually
requires this, I'll be amazed.
···
On Oct 12, 9:59 pm, Brian Adkins <lojicdot...@gmail.com> wrote:
On Oct 12, 1:25 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
> On Oct 12, 2007, at 9:40 AM, Brian Adkins wrote:
> > Actually, what you want is simply N. It's a nonsensical request from a
> > troll. Using max, end, last, etc. to find the end of the range is
> > ridiculous since you must know the end of the range to define the
> > range!
> that is not true. any object can be used in a range in ruby. it
> only must respond to #succ. it's quite possible to declare classes
> that worked like so
I'm not unfamiliar with the domain of the Range class. Let's not get
ridiculous here; I was referring specifically to integer ranges.
Nothing unambiguates like code, eh?
class Range
def max_int
raise 'invalid range' if self.end < self.begin
self.end - (self.exclude_end? ? 1 : 0)
end
end
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
Wrong. (0...1).max_int -> 0 (see code in other post)
you code does not return the max, it returns the 'end', and incorrectly in some cases.
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
there is *no* constraint on ruby ranges to be either - one may use the range to represent either. if one uses it to represent an interval the, with an open ended (... vs ..) range there is no way to compute the max. this is a very useful property because you can use it to do things like
f = 0.42
r = 0 ... 1
if f > r.start and f < r.end
...
so, if we are talking general purpose methods, which the OP was, Range#max cannot be written to be general purpose, even for integer 'ranges'
max = 0.9?
max = 0.99?
max = 0.999?
max = 0.9999?
What the heck are you talking about? Do you really think that 0.9 or
0.99 is in the range (0...1) ?
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
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama
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
1) Range#max is mixed in from Enumerable
2) "When used as an iterator, ranges return each value in the
*sequence*." Pickaxe p. 597
3) An elephant's trunk feels different than its tail
4) "range max conversation".max == this post
···
On Oct 12, 10:42 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
On Oct 12, 10:42 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
On Oct 12, 2007, at 8:05 PM, Brian Adkins 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
1) Range#max is mixed in from Enumerable
2) "When used as an iterator, ranges return each value in the
*sequence*." Pickaxe p. 597
3) An elephant's trunk feels different than its tail
4) "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.
--
Posted via http://www.ruby-forum.com/\.
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.
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
···
On Oct 13, 12:04 am, "ara.t.howard" <ara.t.how...@gmail.com> wrote:
On Oct 12, 2007, at 9:25 PM, Brian Adkins wrote:
> 1) Range#max is mixed in from Enumerable
but #include? is not
> 2) "When used as an iterator, ranges return each value in the
> *sequence*." Pickaxe p. 597
that is true, and here it is an interval
case 0.5
when 0 ... 1
p true
when 1 ... 2
p false
end
they can act as either
> 4) "range max conversation".max == this post
the OP's question is about why the current max is slow. the reason
is because ranges are not always sequences.