nobuyoshi nakada wrote:
> Hi,
>
> At Thu, 27 Oct 2005 15:05:41 +0900,
> Ron M wrote in [ruby-talk:162876]:
>> That sure beats C, but wouldn't it be nice if there
>> were a shorthand for doing so.  I think something
>> like this would be very nice:
>>
>>    people[*].email_addr
>
> What about:
>
>   module Mappable
>     class Mapper
>       def initialize(obj)
>         @obj = obj
>       end
>       def method_missing(meth, *args, &block)
>         @obj.map {|i| i.send(meth, *args, &block)}
>       end
>     end
>     def self.included(klass)
>       super
>       aref = klass.instance_method(:[])
>       klass.module_eval do
>         define_method(:[]) do |*idx|
>           if idx.empty?
>             Mapper.new(self)
>           else
>             aref[*idx]
>           end
>         end
>       end
>     end
>   end
>
>   class Array
>     include Mappable
>   end
>
>   a = %w[a b c]
>   p a[].upcase

I don't like it because now [] does two completely different things:
access and mapping.  I'd prefer something like this:

module Enumerable
  # replacement for map
  def mapx(*a,&b)
    raise "Can only have one" if !a.empty? && b
    if a.empty?
      map(&b)
    else
      map {|x| a.inject(x) {|v,m| v.send(m)}}
    end
  end
end

>> %w{a b c}.mapx :upcase
=> ["A", "B", "C"]
>> %w{a b c}.mapx {|a| a + "x"}
=> ["ax", "bx", "cx"]
>> %w{abc bcd cde}.mapx :upcase, :reverse
=> ["CBA", "DCB", "EDC"]

I just chose mapx to get it working fast, ideally the original map method
of Enumerable classes would be changed.

Kind regards

    robert