Forum: Ruby-core [ruby-trunk - Feature #7793][Open] New methods on Hash

Posted by Dominic S. (dominic_s)
on 2013-02-06 17:53
(Received via mailing list)
Issue #7793 has been reported by dsisnero (Dominic Sisneros).

----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793

Author: dsisnero (Dominic Sisneros)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


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.map_k{|k| k.to_s}
   { "name"=> 'dominic', "request"=>"add the following methods', "why" 
=> "convenience"}

h.map_kv{|k,v| [k.to_s, 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
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2013-02-06 18:07
(Received via mailing list)
Issue #7793 has been updated by marcandre (Marc-Andre Lafortune).

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
----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-35932

Author: dsisnero (Dominic Sisneros)
Status: Closed
Priority: Normal
Assignee:
Category:
Target version:


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.map_k{|k| k.to_s}
   { "name"=> 'dominic', "request"=>"add the following methods', "why" 
=> "convenience"}

h.map_kv{|k,v| [k.to_s, 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
Posted by Dominic S. (dominic_s)
on 2013-02-06 21:02
(Received via mailing list)
Issue #7793 has been updated by dsisnero (Dominic Sisneros).


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

map_v and map_k 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



----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-35944

Author: dsisnero (Dominic Sisneros)
Status: Closed
Priority: Normal
Assignee:
Category:
Target version:


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.map_k{|k| k.to_s}
   { "name"=> 'dominic', "request"=>"add the following methods', "why" 
=> "convenience"}

h.map_kv{|k,v| [k.to_s, 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
Posted by marcandre (Marc-Andre Lafortune) (Guest)
on 2013-02-06 22:57
(Received via mailing list)
Issue #7793 has been updated by marcandre (Marc-Andre Lafortune).

Category set to core
Status changed from Closed to Assigned
Assignee set to matz (Yukihiro Matsumoto)

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

----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-35946

Author: dsisnero (Dominic Sisneros)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version:


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.map_k{|k| k.to_s}
   { "name"=> 'dominic', "request"=>"add the following methods', "why" 
=> "convenience"}

h.map_kv{|k,v| [k.to_s, 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
Posted by charliesome (Charlie Somerville) (Guest)
on 2013-02-06 23:10
(Received via mailing list)
Issue #7793 has been updated by charliesome (Charlie Somerville).


At the risk of bike shedding, I think map_k and map_v should be named 
map_keys and map_values. That can be for matz to decide though.
----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-35947

Author: dsisnero (Dominic Sisneros)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version:


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.map_k{|k| k.to_s}
   { "name"=> 'dominic', "request"=>"add the following methods', "why" 
=> "convenience"}

h.map_kv{|k,v| [k.to_s, 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
Posted by Nobuyoshi Nakada (nobu)
on 2013-02-07 02:25
(Received via mailing list)
Issue #7793 has been updated by nobu (Nobuyoshi Nakada).


Considering existing methods:

  $ ruby -e 'p Hash.instance_methods(false).grep(/each_/)'
  [:each_value, :each_key, :each_pair]

They should be map_key, may_value, and map_pair, respectively, I
think.

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

----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-35955

Author: dsisnero (Dominic Sisneros)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version:


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.map_k{|k| k.to_s}
   { "name"=> 'dominic', "request"=>"add the following methods', "why" 
=> "convenience"}

h.map_kv{|k,v| [k.to_s, 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
Posted by Nobuyoshi Nakada (nobu)
on 2013-02-07 02:26
(Received via mailing list)
Issue #7793 has been updated by nobu (Nobuyoshi Nakada).

Description updated


----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-35956

Author: dsisnero (Dominic Sisneros)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version:


=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.map_k{|k| k.to_s}
  #=> { "name"=> 'dominic', "request"=>"add the following methods', 
"why" => "convenience"}

  h.map_kv{|k,v| [k.to_s, 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
Posted by yhara (Yutaka HARA) (Guest)
on 2013-02-13 10:22
(Received via mailing list)
Issue #7793 has been updated by yhara (Yutaka HARA).

Target version set to next minor


----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-36214

Author: dsisnero (Dominic Sisneros)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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.map_k{|k| k.to_s}
  #=> { "name"=> 'dominic', "request"=>"add the following methods', 
"why" => "convenience"}

  h.map_kv{|k,v| [k.to_s, 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
Posted by Shannon S. (shannon_s)
on 2013-02-16 01:37
Attachment: hashy.rb (329 Bytes)
Just cut a little gem called 'hashy' implementing the above-proposed
Hash#map_pair, Hash#map_key and Hash#map_value.

https://github.com/havenwood/hashy/blob/master/README.md
Posted by Matthew Kerwin (mattyk)
on 2013-03-13 03:13
(Received via mailing list)
Issue #7793 has been updated by phluid61 (Matthew Kerwin).


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 #map_keys, #map_values and #map_pairs as my method names.

----------------------------------------
Feature #7793: New methods on Hash
https://bugs.ruby-lang.org/issues/7793#change-37553

Author: dsisnero (Dominic Sisneros)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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.map_k{|k| k.to_s}
  #=> { "name"=> 'dominic', "request"=>"add the following methods', 
"why" => "convenience"}

  h.map_kv{|k,v| [k.to_s, 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.