From: "David A. Black" Date: 2005-10-29T00:44:29+09:00 Subject: Re: Cleaner syntax for .map (is there already a way, or ruby2 idea?) Hi -- On Fri, 28 Oct 2005, Dave Burt wrote: > David A. Black wrote: >> I continue to struggle to understand what people find so horrible >> about ary.each {|item| .... }, ary.map {|item| ... }, and so forth. >> I'll have some more coffee and maybe I'll start to see the light.... > > There's nothing really wrong with them, but they are verbose in simple (and > common) cases: > > result = array.map {|element| element.transform } > > The only issue is that you're saying "element" twice when you would only say > it once if you were describing the operation to a person. I probably would say "element" at least twice in describing it to a person, I think. "Go through the array one element at a time, call the "transform" method on the current element, and save the result to an accumulator array" or something like that. > Groovy's (optional) implicit parameter is the counterpoint: > > // Groovy: > result = array.map { it.transform() } :-) > Tangentially, I wonder if the proposed Ruby 2 block syntax makes this > slightly more possible. > > # Pseudo-Ruby 2.0: > result = array.map -> { it.transform } :-) I hope that's more pseudo than Ruby 2 :-) David -- David A. Black dblack@wobblini.net