From: Robert Klemme Date: 2005-03-31T20:04:49+09:00 Subject: Re: Syntax sugar idea for loops "Jon Raphaelson" schrieb im Newsbeitrag news:424BBE2E.5000406@gmail.com... > Luke Graham wrote: > > On Thu, 31 Mar 2005 16:54:25 +0900, Lasse Koskela > > wrote: > > > >>On Thu, 31 Mar 2005 16:34:44 +0900, Robert Klemme wrote: > >> > >>>Yeah, "odd" and "even" are much too specific IMHO. Using the index is the > >>>most general solution. > >> > >>True, odd and even are rather specific but is that such a bad thing, > >>really? If the odd-even semantics are a common need, why not add > >>support for it in the language? I mean, why else would Ruby have > >>Array.each etc. instead of for (i=0; i < x; i++) which is clearly more > >>powerful in terms of all the things you can do with it? > > > > > > Internal and external iterators are equivalent given the existance > > of continuations. It may be interesting to explore the possibilities > > of representing flow control as an object. To start the debate... > > > > for () { > > case for.special # Cant think of a better name right now :( > > when for.first > > .. > > when for.last > > .. > > for.restart if error > > else > > ... > > for.next > > puts "We get here, unlike the real next keyword" > > end > > } > > > > Please note, I am -not- suggesting these for Ruby. I dont even like the > > odd/even idea personally. Im brainstorming to see what is possible and > > would like to see what others can come up with as well. > > > > This seems to me like something of what generators are in python, which > is just a function that yields instead of returns. I'm not an expert, > but they are really powerful and useful for defining custom iterators > and the like. > > This generator is basically an open ended list of integers that vary by > 2 ( I would like to say that I use ruby and not python, and I'm not > really fluent in python. So if this is wrong, will someone correct me > please? ) > > > > def every_other(num): > while 1: > num += 2 > yield num > > > odds = every_other(3) > > odds.next() #=> 5 > odds.next() #=> 7 > odds.next() #=> 9 > odds.next() #=> 11 > odds.next() #=> 13 > odds.next() #=> 15 > > .. and so on. > > I really like the idea of generators, and think that something like them > explicity might bee useful in ruby. I know it would be not-quite-trivial > to write a generator class (and there might be one already,) but they > are a nifty language feature that python has going for it. It's been done already for arbitrary Enumerations: http://www.ruby-doc.org/core/classes/Generator.html It's trivial for odd or even numbers. robert