Improving min/max for Range

A

Alexander

Range relies entirely on Enumerable to implement #min and #max, which
in turn implements these operations using #to_a. This becomes a
problem with ranges of floats, for example:
TypeError: can't iterate from Float
from (irb):9:in `each'
from (irb):9
from :0

Not to mention that it becomes inefficient to expand the whole range
into array, such as for very large integers:

(Tends to run out of memory.)

I think it makes more sense to implement these operations in terms of
<=3D>, like so:

class Range

def min
(self.first <=3D> self.last) <=3D 0 ? self.first : nil
end

def max
(self.first <=3D> self.last) <=3D 0 ? self.last : nil
end

end

I don't see any reason to rely on expanding the range using
Enumerable#to_a, since the operations can be implemented purely in
terms of the range boundaries.

I would be happy to submit a patch.

Alexander.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Improving min/max for Range"

|I think it makes more sense to implement these operations in terms of
|<=>, like so:

#min makes sense. I'd like to merge your idea. #max needs to
consider about end exclusion. I'm not sure what (0...1.0).max should
return. Error?

matz.
 
A

Alexander

Hi,

In message "Re: Improving min/max for Range"
|I think it makes more sense to implement these operations in terms of
|<=3D>, like so:

#min makes sense. I'd like to merge your idea. #max needs to
consider about end exclusion. I'm not sure what (0...1.0).max should
return. Error?

matz.

Good point. I think max should throw if the number doesn't support the
integer protocol; there's simply no way to handle it intelligently.
For int-type types, I suggest that (end.to_int - 1) should be used,
since there is no #pred counterpart to #succ.

Alexander.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Improving min/max for Range"

|> #min makes sense. I'd like to merge your idea. #max needs to
|> consider about end exclusion. I'm not sure what (0...1.0).max should
|> return. Error?

|Good point. I think max should throw if the number doesn't support the
|integer protocol; there's simply no way to handle it intelligently.
|For int-type types, I suggest that (end.to_int - 1) should be used,
|since there is no #pred counterpart to #succ.

I've just committed my implementation of Range#min and #max to CVS
HEAD. Check if you have interest.

matz.
 
A

Alexander

|Good point. I think max should throw if the number doesn't support the
|integer protocol; there's simply no way to handle it intelligently.
|For int-type types, I suggest that (end.to_int - 1) should be used,
|since there is no #pred counterpart to #succ.

I've just committed my implementation of Range#min and #max to CVS
HEAD. Check if you have interest.

matz.

I see you decided to return nil instead of throwing; acceptable, I
suppose. More sneaky is how you let Enumerable handle the case when
the end is excluded -- I would have preferred to do that only do if
#end was not an integer.

Here's a unit test patch, btw:

diff -u -r1.5 test_range.rb
--- test/ruby/test_range.rb 5 Aug 2005 23:56:02 -0000 1.5
+++ test/ruby/test_range.rb 25 Nov 2005 02:47:06 -0000
@@ -31,4 +31,24 @@
def test_duckrange
assert_equal("bc", "abcd"[DuckRange.new(1,2)])
end
+
+ def test_min
+ assert_equal(1, (1..2).min)
+ assert_equal(nil, (2..1).min)
+ assert_equal(1, (1...2).min)
+
+ assert_equal(1.0, (1.0..2.0).min)
+ assert_equal(nil, (2.0..1.0).min)
+ assert_equal(1, (1.0...2.0).min)
+ end
+
+ def test_max
+ assert_equal(2, (1..2).max)
+ assert_equal(nil, (2..1).max)
+ assert_equal(1, (1...2).max)
+
+ assert_equal(2.0, (1.0..2.0).max)
+ assert_equal(nil, (2.0..1.0).max)
+ assert_raise(TypeError) { (1.0...2.0).max }
+ end
end

Alexander.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Improving min/max for Range"

|I see you decided to return nil instead of throwing; acceptable, I
|suppose. More sneaky is how you let Enumerable handle the case when
|the end is excluded -- I would have preferred to do that only do if
|#end was not an integer.

Sounds reasonable. Can you check my new modify?

|Here's a unit test patch, btw:

Thank you. It's merged.

matz.
 
A

Alexander

Hi,

In message "Re: Improving min/max for Range"
|I see you decided to return nil instead of throwing; acceptable, I
|suppose. More sneaky is how you let Enumerable handle the case when
|the end is excluded -- I would have preferred to do that only do if
|#end was not an integer.

Sounds reasonable. Can you check my new modify?

Great, that works. Thanks.

Alexander.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top