From: serialhex Date: 2012-04-14T12:27:08+09:00 Subject: Re: unable to do "down" range? --bcaec553fe68c4cb4504bd9b2906 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable snt frum my awesum ansroid... txt mite b rong On Apr 13, 2012 5:04 PM, "Ralph Shnelvar" wrote: > Roger and Jan, > > Friday, April 13, 2012, 1:23:54 PM, you wrote: > > JE> Hi, > > JE> No, this doesn't work, because 40..10 is an empty range. A range > JE> consists of all objects between the left limit and the right limit. > > JE> We've already discussed this topic here (I cannot find it right now). > My > JE> personal opinion is that many people are confusing ranges with > JE> sequences, because the ".." syntax looks like you're enumerating > JE> numbers: > > 4..10 =3D>> 4, 5, 6, ..., 10 > > JE> But a range isn't a sequence. You're not saying "count from ... to > ...". > JE> The numbers are rather limits of a "static" interval. So there's isn'= t > a > JE> real direction implied, the elements are simply put out in the most > JE> obvious way: from low to high. > > JE> I think Integer#downto is what you're looking for: > > JE> 40.downto 10 do |i| > JE> puts i > JE> end > > I have thought about this problem and I think I may make my contribution > to the Ruby language an extension that would allow negative ranges as you > describe. > > There are problems, though. > > How many pieces of code would break if > range =3D 40..10 > range.each{|n| puts n} # it doesn't > actually did what you want? > > Probably not many. > > > > The documentation in http://www.ruby-doc.org/core-1.9.3/Range.html says > each {| i | block } =E2=86=92 rng click to toggle source > each =E2=86=92 an_enumerator > > Iterates over the elements rng, passing each in turn to the block. You > can only iterate if the start object of the range supports the succ metho= d > (which means that you can=E2=80=99t iterate over ranges of > Float objects). > > To do what Roger wants, we would need to define a prev function (similar > to the succ function) on Fixnums. > > 3.succ # 4 > 4,prev # Undefined! > > > Fortunately, this is easy to do in Ruby > > class Fixnum > def prev > self - 1 > end > end > > 4.prev # 3 > > > > So, should we clutter Ruby with a collection of prev functions (because > we'd also need to do it for all the things that succ is defined on)? > Possibly break existing code? > > I'd vote yes. > > There would be at least two advantages to having a well-defined way of > dealing with negative ranges beyond the fact that we'd have the relativel= y > minor advantage of having a negative range. > > As I understand the code right now, > > (1..10_000_000).last(5) # [9999996, 9999997, 9999998, 9999999, 10000000] > > will create a temporary array of ten million elements and then peel off > the last five array elements. > > Now from my perspective, having to allocate that relatively huge temporar= y > array is awful; truly Rube Goldberg-esque. Implementing a prev function > that would operate on the "end object" (in this instance, 10_000_000) the > way that succ works on the "start object" would eliminate the need for th= at > temporary array. If we had that in the implementation of Ruby, then we > could get access to the last five elements more-or-less directly. > > Ralph Shnelvar > > > > --bcaec553fe68c4cb4504bd9b2906 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

snt frum my awesum ansroid... txt mite b rong

On Apr 13, 2012 5:04 PM, "Ralph Shnelvar&qu= ot; <ralphs@dos32.com> wrote:=
Roger and Jan,

Friday, April 13, 2012, 1:23:54 PM, you wrote:

JE> Hi,

JE> No, this doesn't work, because 40..10 is an empty range. A range=
JE> consists of all objects between the left limit and the right limit.<= br>
JE> We've already discussed this topic here (I cannot find it right = now). My
JE> personal opinion is that many people are confusing ranges with
JE> sequences, because the ".." syntax looks like you're e= numerating
JE> numbers:

4..10 =3D>> 4, 5, 6, ..., 10

JE> But a range isn't a sequence. You're not saying "count = from ... to ...".
JE> The numbers are rather limits of a "static" interval. So t= here's isn't a
JE> real direction implied, the elements are simply put out in the most<= br> JE> obvious way: from low to high.

JE> I think Integer#downto is what you're looking for:

JE> 40.downto 10 do |i|
JE> =C2=A0 puts i
JE> end

I have thought about this problem and I think I may make my contribution to= the Ruby language an extension that would allow negative ranges as you des= cribe.

There are problems, though.

How many pieces of code would break if
=C2=A0range =3D 40..10
=C2=A0range.each{|n| puts n} # it doesn't
actually did what you want?

Probably not many.



The documentation in http://www.ruby-doc.org/core-1.9.3/Range.html say= s
=C2=A0each {| i | block } =E2=86=92 rng click to toggle source
=C2=A0each =E2=86=92 an_enumerator

=C2=A0Iterates over the elements rng, passing each in turn to the block. Y= ou can only iterate if the start object of the range supports the succ meth= od (which means that you can=E2=80=99t iterate over ranges of
=C2=A0Float objects).

To do what Roger wants, we would need to define a prev function (similar to= the succ function) on Fixnums.

=C2=A03.succ # 4
=C2=A04,prev # Undefined!


Fortunately, this is easy to do in Ruby

=C2=A0class Fixnum
=C2=A0 =C2=A0def prev
=C2=A0 =C2=A0 =C2=A0self - 1
=C2=A0 =C2=A0end
=C2=A0end

=C2=A04.prev # 3



So, should we clutter Ruby with a collection of prev functions (because we&= #39;d also need to do it for all the things that succ is defined on)? =C2= =A0Possibly break existing code?

I'd vote yes.

There would be at least two advantages to having a well-defined way of deal= ing with negative ranges beyond the fact that we'd have the relatively = minor advantage of having a negative range.

As I understand the code right now,

=C2=A0(1..10_000_000).last(5) # [9999996, 9999997, 9999998, 9999999, 10000= 000]

will create a temporary array of ten million elements and then peel off the= last five array elements.

Now from my perspective, having to allocate that relatively huge temporary = array is awful; truly Rube Goldberg-esque. =C2=A0Implementing a prev functi= on that would operate on the "end object" (in this instance, 10_0= 00_000) the way that succ works on the "start object" would elimi= nate the need for that temporary array. =C2=A0If we had that in the impleme= ntation of Ruby, then we could get access to the last five elements more-or= -less directly.

Ralph Shnelvar



--bcaec553fe68c4cb4504bd9b2906--