From: "David A. Black" Date: 2004-04-05T21:17:15+09:00 Subject: Re: deciding between ruby and python Hi -- On Mon, 5 Apr 2004, Charles Comstock wrote: > > Perhaps some of us like to do our iteration explicitly. I don't think > > there is any syntactic overhead, and the for loop reads > > better. Counterexamples welcome. > > > > Obviously we can do #each too: > > > > def L(x): > > print x > > return x*2 > > > > map(L, [1,2,3]) > > > (reverse (map (lambda (x) (begin (display x) (* x 2))) > (map (lambda (x) (+ x 2)) (list 1 2 3)))) > > [1,2,3].each {|x| x+2}.each {|x| print x;return x*2}.reverse I think you want #map there. With each, the first block is essentially a no-op. Also, return will break through all levels (including method call): def m [1,2].each {|x| puts x; return} end m # => 1 > or if you prefer > > [1,2,3].each {|x| x+2}.each do |x| > print x > x*2 > end.reverse Again, with #each rather than #map, you're just throwing away the results of the calculations (x+2 and x*2). [...] > a = [1,2,3] > a = for x in a > print x > x + 2 > end.reverse > > In ruby you don't need to choose between map and for if you want the > result list, you can do whichever pleases you the most. for doesn't generate a result list though. What you've got here will return [3,2,1], and all the x+2 calculations will be discarded (like #each). David -- David A. Black dblack@wobblini.net