Hi --

On Thu, 2 Oct 2008, Robert Klemme wrote:

> 2008/10/1 David A. Black <dblack / rubypal.com>:
>>> In that case #map might be more appropriate - at least if the target
>>> collection is an Array.  Btw, did we ever discuss having #map accept a
>>> parameter which defaults to []?  i.e.
>>>
>>> module Enumerable
>>>  def map(target = [])
>>>   each {|x| target << yield x}
>>>   target
>>>  end
>>> end
>>
>> I don't think it's a good idea; it generalizes the idea of a mapping
>> of a collection beyond anything that really seems to me to be a
>> mapping. If I saw:
>>
>>  [1,2,3,4,5]   =>  "Hi."
>>
>> I would not just say it's a weird mapping; I would not be able to
>> identify it as a mapping at all. It's a more general transformation.
>
> I'm not sure I understand exactly what you mean.  My point was simply
> to allow the caller to provide the target collection where something
> is mapped to.  Of course you can then also use a String or anything
> else that supports << (a stream for example) which I find quite neat.
> But where does "=>" syntax come into play?

It doesn't; I'm just using it to separate the collection and a
potential "mapping".

   [1,2,3,4,5].map("") {...}   # Result:  "Hi."

I don't think that << correctly represents the concept of mapping, so
I would not want map to be generalized to any <<-capable target
object. It's more a <<'ing, or something, than a mapping. It happens
that the current behavior of map can be implemented using << and an
empty array, but I don't think that means that << per se is at the
heart of mapping.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!