[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A left-exclusive Range class

Cyrus Hall

8/2/2006 2:15:00 PM

The current Range class in ruby is quite useful, however, I was recently
trying to capture a simple algorithm and ended up needing to express
left-exclusion. This is the one feature ruby currently does not support
in its Range class.

I did some digging into 1.8.4's Range implementation and it appears
that the changes needed to support left-exclusion would be minimal.
Rather, the problem seems syntactical. Currently '..' and '...' are
used to represent right-inclusion and -exclusion, respectively. This
syntax clearly can not be extended to left-exclusion. There appears to
have been a related debate about this on ruby-talk back in Oct. 2004,
with the conclusion being, to paraphrase the entire discussion, "this is
hard, lets forget about it for now."

Has this been discussed since 2004? Would people be interested in
finding a way to get left-exclusion into the Range class? This may be
something that has been addressed in all the language work going into
1.9/2.0, and if so, any pointers would be greatly appreciated.

Ciao,
Cyrus Hall



16 Answers

Brian Guthrie

8/3/2006 2:56:00 PM

0

Cyrus Hall wrote:
> The current Range class in ruby is quite useful, however, I was recently
> trying to capture a simple algorithm and ended up needing to express
> left-exclusion. This is the one feature ruby currently does not support
> in its Range class.
>
> I did some digging into 1.8.4's Range implementation and it appears
> that the changes needed to support left-exclusion would be minimal.
> Rather, the problem seems syntactical. Currently '..' and '...' are
> used to represent right-inclusion and -exclusion, respectively. This
> syntax clearly can not be extended to left-exclusion. There appears to
> have been a related debate about this on ruby-talk back in Oct. 2004,
> with the conclusion being, to paraphrase the entire discussion, "this is
> hard, lets forget about it for now."
>
> Has this been discussed since 2004? Would people be interested in
> finding a way to get left-exclusion into the Range class? This may be
> something that has been addressed in all the language work going into
> 1.9/2.0, and if so, any pointers would be greatly appreciated.
>
> Ciao,
> Cyrus Hall

You could try just appropriating a different piece of syntax and using
it for your own evil purposes, e.g. <<

class Fixnum
def <<(number)
(self + 1)..number
end
end

for i in 1<<10
# i starts at 2
end

Brian Guthrie

Brian Guthrie

8/3/2006 2:58:00 PM

0

Brian Guthrie wrote:
> Cyrus Hall wrote:
>> The current Range class in ruby is quite useful, however, I was recently
>> trying to capture a simple algorithm and ended up needing to express
>> left-exclusion. This is the one feature ruby currently does not support
>> in its Range class.
>>
>> I did some digging into 1.8.4's Range implementation and it appears
>> that the changes needed to support left-exclusion would be minimal.
>> Rather, the problem seems syntactical. Currently '..' and '...' are
>> used to represent right-inclusion and -exclusion, respectively. This
>> syntax clearly can not be extended to left-exclusion. There appears to
>> have been a related debate about this on ruby-talk back in Oct. 2004,
>> with the conclusion being, to paraphrase the entire discussion, "this is
>> hard, lets forget about it for now."
>> Has this been discussed since 2004? Would people be interested in
>> finding a way to get left-exclusion into the Range class? This may be
>> something that has been addressed in all the language work going into
>> 1.9/2.0, and if so, any pointers would be greatly appreciated.
>>
>> Ciao,
>> Cyrus Hall
>
> You could try just appropriating a different piece of syntax and using
> it for your own evil purposes, e.g. <<
>
> class Fixnum
> def <<(number)
> (self + 1)..number
> end
> end
>
> for i in 1<<10
> # i starts at 2
> end
>
> Brian Guthrie
>

Of course by doing that you've totally just broken the existing
Fixnum#<<. Bummer, should've checked first. Ah, well.

Brian Guthrie

Mike Fletcher

8/3/2006 3:21:00 PM

0

Brian Guthrie wrote:
> Cyrus Hall wrote:
>> have been a related debate about this on ruby-talk back in Oct. 2004,
>> with the conclusion being, to paraphrase the entire discussion, "this is
>> hard, lets forget about it for now."
>>
>> Has this been discussed since 2004? Would people be interested in
>> finding a way to get left-exclusion into the Range class? This may be
>> something that has been addressed in all the language work going into
>> 1.9/2.0, and if so, any pointers would be greatly appreciated.

Doesn't preserve the fact that it started as a left exclusive range, but
how about:

class Range
alias :orig_initialize :initialize
def initialize( s, e, exclusive=false )
case exclusive
when false, true
orig_initialize( s, e, exclusive )
when :left
orig_initialize( s.succ, e )
when :right
orig_initialize( s, e, true )
when :both
orig_initialize( s.succ, e, true )
end
end
end

Then you can do Range.new( 1, 5, :left ) or Range.new( 1, 5, :right ) or
use the .. / ... operators as well.

--
Posted via http://www.ruby-....

Graham Woodland

1/31/2009 2:40:00 PM

0

Joy Beeson wrote:
> On Fri, 30 Jan 2009 18:02:15 -0800, David Friedman
> <ddfr@daviddfriedman.nopsam.com> wrote:
>
>> I was trying to keep it down to four
>> ingredients, which I think is the number someone earlier in the thread
>> mentioned.
>
> Before my doctor declared white potatoes a special treat,

Ouch! You have my sympathy. But why white potatoes? Is this in
contradistinction to red?


my favorite
> emergency lunch was potato soup:
>

Saved for a rainy day.


--
Cheers,

Gray

---
To unmung address, lop off the 'be invalid' command.

J.Pascal

1/31/2009 3:58:00 PM

0

On Jan 31, 7:40 am, Graham Woodland <g...@graeghama.plus.comb.invalid>
wrote:
> Joy Beeson wrote:
> > On Fri, 30 Jan 2009 18:02:15 -0800, David Friedman
> > <d...@daviddfriedman.nopsam.com> wrote:
>
> >> I was trying to keep it down to four
> >> ingredients, which I think is the number someone earlier in the thread
> >> mentioned.
>
> > Before my doctor declared white potatoes a special treat,
>
> Ouch!  You have my sympathy.  But why white potatoes?  Is this in
> contradistinction to red?

I think it's "not a sweet potato or yam".

-Julie

Irina Rempt

1/31/2009 10:58:00 PM

0

On Saturday 31 January 2009 23:54, John Park wrote:

> "Suzanne Blom" (sueblom@execpc.com) writes:
> [...]
>> Pinto beans, cooked, spooned down the middle of each (Okay, I'm
>> eccentric; I almost always have cooked pinto beans in the frig, but I
>> suppose you could use canned.)
> [...]
>
> You would not believe how much time I spent (this being an sf group)
> wondering how to cook beans in the refrigerator....

yes, me too!

Irina

--
"Of course it is happening inside your head, Harry, but why on earth
should that mean that it is not real?" --Albus Dumbledore
http://www.valdyas.org/foundobjects... Latest: 29-Jan-2009

David Friedman

2/1/2009 1:48:00 AM

0

In article <gm2kqi$djr$1@theodyn.ncf.ca>,
af250@FreeNet.Carleton.CA (John Park) wrote:

> "Suzanne Blom" (sueblom@execpc.com) writes:
> [...]
> > Pinto beans, cooked, spooned down the middle of each (Okay, I'm eccentric;
> > I
> > almost always have cooked pinto beans in the frig, but I suppose you could
> > use canned.)
> [...]
>
> You would not believe how much time I spent (this being an sf group)
> wondering how to cook beans in the refrigerator....

It's a typo.

A refrigerator is a heat pump. You don't cook beans in it, you cook
beans on it.

--
http://www.daviddfri... http://daviddfriedman.blo...
Author of
_Future Imperfect: Technology and Freedom in an Uncertain World_,
Cambridge University Press.

Joy Beeson

2/2/2009 2:24:00 AM

0

On Sat, 31 Jan 2009 07:57:42 -0800 (PST), julie@pascal.org wrote:

> I think it's "not a sweet potato or yam".

Right on. Blue potatoes and Yukon Gold potatoes are also white
potatoes, as far as our diet is concerned.

To answer the preceding post's other question, my doctor believes that
all old people should eat as if they were diabetic. Especially
spouses who were temporarily diabetic as a side-effect of medication
that kept him from going blind.

On the bright side, he also believes that dark chocolate is good for
you. Also blueberries and bell peppers and fresh-ground whole-grain
flour. (Ground last summer, but I keep it in the deep freeze.)

Joy Beeson
--
joy beeson at comcast dot net
http://roughsewing.home.co... -- sewing
http://n3f.home.co... -- Writers' Exchange
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.

Brian M. Scott

2/2/2009 2:45:00 AM

0

On Sat, 31 Jan 2009 07:57:42 -0800 (PST), <julie@pascal.org>
wrote in
<news:0f3b3c2d-046e-4981-a491-7752efb4ac07@b38g2000prf.googlegroups.com>
in rec.arts.sf.composition:

> On Jan 31, 7:40 am, Graham Woodland <g...@graeghama.plus.comb.invalid>
> wrote:

>> Joy Beeson wrote:

[...]

>>> Before my doctor declared white potatoes a special treat,

>> Ouch!  You have my sympathy.  But why white potatoes?  Is
>> this in contradistinction to red?

> I think it's "not a sweet potato or yam".

Ah. It would never have occurred to me that either of those
might be considered potatoes. (But then I don't consider
either of them edible, save to prevent starvation.)

Brian

Graham Woodland

2/2/2009 5:54:00 AM

0

Brian M. Scott wrote:
> On Sat, 31 Jan 2009 07:57:42 -0800 (PST), <julie@pascal.org>
> wrote in
> <news:0f3b3c2d-046e-4981-a491-7752efb4ac07@b38g2000prf.googlegroups.com>
> in rec.arts.sf.composition:
>
>> On Jan 31, 7:40 am, Graham Woodland <g...@graeghama.plus.comb.invalid>
>> wrote:
>
>>> Joy Beeson wrote:
>
> [...]
>
>>>> Before my doctor declared white potatoes a special treat,
>
>>> Ouch! You have my sympathy. But why white potatoes? Is
>>> this in contradistinction to red?
>
>> I think it's "not a sweet potato or yam".
>
> Ah. It would never have occurred to me that either of those
> might be considered potatoes.

Me neither. Much is explained hereby.

(But then I don't consider
> either of them edible, save to prevent starvation.)
>

I think yam does very well in a good curry. There's an excellent
hot sharp vegetarian one in which it co-stars with green mangoes;
and to anybody living within shouting distance of Bangor,
Gwynedd, there is a bar there called the Fat Cat which does a
rather fine job involving (IIRC) yam and red snapper. Actually
it does pretty well with quite a lot of stuff. It's part of a
smallish chain, though I've never seen another one.


--
Cheers,

Gray

---
To unmung address, lop off the 'be invalid' command.