Project

General

Profile

Actions

Feature #7297

closed

map_to alias for each_with_object

Added by nathan.f77 (Nathan Broadbent) over 11 years ago. Updated over 11 years ago.

Status:
Rejected
Assignee:
-
Target version:
[ruby-core:48997]

Description

I would love to have a shorter alias for 'each_with_object', and would like to propose 'map_to'. Here are my arguments:

  • It reads logically and clearly:

[1, 2, 3].map_to({}) {|i, hash| hash[i] = i ** 2 }
#=> {1 => 1, 2 => 4, 3 => 9}

  • Rubyists are already using 'map' to build and return an array, so it should be obvious that 'map_to(object)' can be used to build and return an object.

  • Given that 'each' and 'each_with_index' return the original array, I feel that the 'each_with_object' method name is slightly counterintuitive. 'map_to' might not be 100% semantically correct, but it's obvious that it will return something other than the original array.

  • Many people (myself included) were using inject({}) {|hash, el| ... ; hash } instead of 'each_with_object', partly because of ignorance, but also because 'each_with_object' is so long. 'map_to' is the same length as inject, and means that you don't have to return the object at the end of the block.

  • Only a single line of code is needed to implement the alias.

Best,
Nathan


Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #7340: 'each_with' or 'into' alias for 'each_with_object'OpenActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0