Feature #7793

New methods on Hash

Added by Dominic Sisneros about 1 year ago. Updated about 1 year ago.

[ruby-core:51933]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor

Description

=begin
It would be nice to have the following methods added to hash

h = { name: 'dominic', request: 'add the following methods', :why => 'convenience'}

h.map_v{|v| v.upcase}
#=> {:name=>"DOMINIC", :request=>"ADD THE FOLLOWING METHODS", :why=>"CONVENIENCE"}

h.mapk{|k| k.tos}
#=> { "name"=> 'dominic', "request"=>"add the following methods', "why" => "convenience"}

h.mapkv{|k,v| [k.tos, v.upcase]}
#=> { "name"=>"DOMINIC", "request"=>"ADD THE FOLLOWING METHODS", "why"=>"CONVENIENCE"}

class Hash

def map_v
  reduce({}) do |result, array|
    k,v = array
    new_val = yield v
    result.merge( k => new_val)
  end
end

def map_k
  reduce({}) do |result, array|
    k,v = array
    new_k = yield k
    result.merge(new_k => v)
  end
end

def map_kv
  reduce({}) do |result, array|
    new_k,new_v = yield array
    result.merge(new_k => new_v)
  end
end

end
=end


Related issues

Related to ruby-trunk - Feature #6669: A method like Hash#map but returns hash Feedback 06/30/2012
Related to ruby-trunk - Feature #4151: Enumerable#categorize Assigned
Related to ruby-trunk - Feature #7292: Enumerable#to_h Closed 11/07/2012

History

#1 Updated by Marc-Andre Lafortune about 1 year ago

  • Status changed from Open to Closed

I am glad to see that more people like you take the time to propose ways to create hashes.

I completely agree that hash creation from Enumerable is lacking currently.

I will close this feature request because I am convinced it can't be accepted as is (the proposed names have no chance of being accepted) and because it is largely duplicated by the following:

https://bugs.ruby-lang.org/issues/6669
https://bugs.ruby-lang.org/issues/4151
https://bugs.ruby-lang.org/issues/7292

If you have the time, read on those and see if you can contribute.

Thanks

#2 Updated by Dominic Sisneros about 1 year ago

This should be re-opened. It is not for all enumerables but only for hash.

mapv and mapk are very useful

map_kv is similar to h.mash and others and could be eliminated by those other bugs but the other functions aren't and are specifically for hashes and thus this should be re-opened

#3 Updated by Marc-Andre Lafortune about 1 year ago

  • Category set to core
  • Status changed from Closed to Assigned
  • Assignee set to Yukihiro Matsumoto

Fine, I'll reopen and assign this to Matz.

#4 Updated by Charlie Somerville about 1 year ago

At the risk of bike shedding, I think mapk and mapv should be named mapkeys and mapvalues. That can be for matz to decide though.

#5 Updated by Nobuyoshi Nakada about 1 year ago

=begin
Considering existing methods:

$ ruby -e 'p Hash.instancemethods(false).grep(/each/)'
[:eachvalue, :eachkey, :each_pair]

They should be (({mapkey})), (({mayvalue})), and (({map_pair})), respectively, I
think.

Anyway, why don't you make it a gem first?
=end

#6 Updated by Nobuyoshi Nakada about 1 year ago

  • Description updated (diff)

#7 Updated by Yutaka HARA about 1 year ago

  • Target version set to next minor

#8 Updated by Matthew Kerwin about 1 year ago

nobu (Nobuyoshi Nakada) wrote:

Anyway, why don't you make it a gem first?

That's a good idea. Let's see what the uptake is, if any: https://rubygems.org/gems/hashmap

Note: I used #mapkeys, #mapvalues and #map_pairs as my method names.

Also available in: Atom PDF