Hi --

On Fri, 15 Nov 2002, Aleksei Guzev wrote:

>
> ######################################################
>
> module Enumerable
>     def collectm(*args,&proc)
>         result = []
>         each_with_index{|e,i| res << yield(e,*args.collect{|f| f[i]})}
>         return result
>     end
> end

Two questions, and some further hacking:

1. Why "collectm" as the name?
2. Are you sure you want the block to be mandatory?  (See my Array#braid
   and a couple of other posts for versions that have a non-block
   option.)

Further hacking:

You could dispense with your temporary variable if you wanted:

  def collectm(*args)
     (0...size).map {|i| yield([self,*args].map {|a| a[i]})}
   end

This is also a good example of why I wish we had map_with_indices.
Hey, wait a minute, we do:

  module Enumerable
    def map_with_indices
      (0...size).each {|i| yield(at(i),i)}
    end
  end

Now your method could be:

  module Enumerable
    def collectm(*args)
      map_with_indices {|e,i| yield(e,*args.map {|a| a[i]})}
    end
  end

which doesn't really change much but I think is cool :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav