range max

  • Thread starter Marek Kasperkiewicz
  • Start date
B

Brian Adkins

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
 
B

Brian Adkins

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
 
L

Luu Trung

Marek said:
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
 
L

Luu Trung

Brian said:
Fixed now :)


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.
 
A

ara.t.howard

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

p ( -1 .. -4 ).max

;-)

a @ http://codeforpeople.com/
 
A

ara.t.howard

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.

regards.

a @ http://codeforpeople.com/
 
B

Brian Adkins

but #include? is not


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


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 :)
 
A

ara.t.howard

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.

cheers.

a @ http://codeforpeople.com/
 
R

Rick DeNatale

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,348
Members
48,026
Latest member
ArnulfoCat

Latest Threads

Top