
module Enumerable

  # Same as #collect but with an iteration counter.
  #
  #   a = [1,2,3].collect_with_index { |e,i| e*i }
  #   a  #=> [0,2,6]
  #
  def collect_with_index
    r = []
    each_index do |i|
      r << yield(self[i], i)
    end
    r
  end

  alias_method( :map_with_index, :collect_with_index )

  #--
  # Why the term counter? There may be a change in Ruby 2.0
  # to use this word instead of index. Index will
  # still be used for Array, since that is the proper meaning
  # in that context. In the mean time, aliases are provided.
  #++
  alias_method( :collect_with_counter, :collect_with_index )
  alias_method( :map_with_counter, :collect_with_index )

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_collect_with_index
      a = [1,2,3].collect_with_index{ |e,i| e*i }
      assert_equal( [0,2,6], a )
    end

  end

=end
