Range max

D’oh! Old code failed for (0…0)

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

Marek K. wrote:

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?

text

On Oct 12, 10:42 pm, “ara.t.howard” [email protected] wrote:

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 :slight_smile:

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

Brian A. wrote:

On Oct 12, 10:42 pm, “ara.t.howard” [email protected] wrote:

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 :slight_smile:

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.

On Oct 12, 2007, at 9:05 PM, Brian A. wrote:

  self.exclude_end? ? self.end - 1 : self.end
else
  orig_max
end

end
end

p ( -1 … -4 ).max

:wink:

a @ http://codeforpeople.com/

Marek K. wrote:

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?

sfsfa

On Oct 13, 12:04 am, “ara.t.howard” [email protected] wrote:

  1. “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.

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 :slight_smile:

On Oct 13, 12:02 am, “ara.t.howard” [email protected] wrote:

     self.end.kind_of?(Integer) &&

p ( -1 … -4 ).max

:wink:

Are you complaining about my version of max or the built in? I can’t
tell because the results are identical :wink:

On Oct 12, 2007, at 9:25 PM, Brian A. wrote:

  1. Range#max is mixed in from Enumerable

but #include? is not

  1. “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

  1. “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.

regards.

a @ http://codeforpeople.com/

On 10/12/07, ara.t.howard [email protected] wrote:

On Oct 12, 2007, at 9:40 AM, Brian A. wrote:

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.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Oct 12, 2007, at 10:50 PM, Brian A. wrote:

Are you complaining about my version of max or the built in? I can’t
tell because the results are identical :wink:

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.

cheers.

a @ http://codeforpeople.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs