From: Trans Date: 2008-10-02T03:38:13+09:00 Subject: Re: Mode method for Array On Oct 1, 12:17 pm, "David A. Black" wrote: > Hi -- > > > > On Thu, 2 Oct 2008, Robert Klemme wrote: > > 2008/10/1 David A. Black : > >>> 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. Why not? Building up a collection requires some means of "building up", and #<< is that means. Standardizing around that method allows for a more comprehensible and flexible system. Is it a semantic thing for you? Would it help to think of #collect, instead of #map? In any case, Robert's idea had to do with providing an initial collection with which to build, whether #<< is used to do that or not. T.