From: SonOfLilit Date: 2007-02-20T02:56:13+09:00 Subject: Re: For loops don't count down In math classes, I've seen cases where it's very comfortable that Sigma (n = b to a) = 0 if b > a. It was just nice to define something as an empty sum this way (generalization of solutions). It's also more according to for(i = a; i < 5; i++) behavior, which is something very useful. Perhaps we should have syntax for both types. Or just Range#arrange (awul name) where (a..b).arrange would return a..a if a wrote: > On Mon, 2007-02-19 at 22:38 +0900, Yukihiro Matsumoto wrote: > > Hi, > > > > In message "Re: For loops don't count down" > > on Mon, 19 Feb 2007 22:27:02 +0900, SonOfLilit writes: > > > > |Numeric#step counts down: > > | > > |5.step(-1) do |i| > > | #stuff > > |end > > > > 5.downto(1) do |i| > > end > > Is there a necessity for a range such as 5..0 to be empty? It would > otherwise be easy to make iteration work for such cases. This is a > simplified solution that doesn't take into account #exclude_end? > > class Range > def each > obj = first > if first <= last > while obj <= last > yield obj > obj = obj.succ > end > else > while first >= last > yield obj > obj = obj.pre > end > end > end > end > > This would of course require that the objects in such a "reverse" range > respond to #pre, or whatever other name would be more suitable. > > I'm basically asking if there is a reason why we shouldn't expand the > functionality of Range#each, other than backwards compatibility (not > that that isn't a big consideration.) > > > Cheers, > Daniel Schierbeck > > >