Submitted by grenzi (Tue Feb 17 18:02:30 UTC 2004)
for i in enumer do puts i if i%2 ==1 end
work like:
enum.map {|i|print i if i%2==1}.compact
By changin this, you get list comprehension-like features. If this RCR is accepted, we may have code like this:
def select_p(list) for p in list: p if p.cond? end end
The for..end loop now actually returns something useful, and is not anymore just a duplication of each().
(actually, that would more look like generator expression as suggested for python 2.4, more than ZFLC)
this change requires changes to the semantyc of the language, and have no implementation.
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
err, obviously it was 'puts' even on the second code box. And obviously the example with select_p() is already doable with just compact. Think it like
g.renziActually I would do your original select_p with select, not compact. All you'd have to do is:
And the second one I would do like this:(I know it has more characters, but I don't think that's an important point in this case.)
In general, I'd rather use (and encourage use of) Ruby's more Ruby-like list processing methods, than add a somewhat obscure functionality into 'for'. (Actually I'd be happy if 'for' disappeared, but that's another story :-)
David Black
My main objection is performance: since current uses don't use a return value for for..end each iteration creates a temporary array and data within it that is discarded immediately.
If one needs map one can use map/collect and it will be clear immediately what's happening there while people usually not expect to get something back from a for loop statement IMHO.
Robert Klemme