ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: Ruby-Lang@JoergWMittag.De
To: ruby-core@ruby-lang.org
Subject: [ruby-core:76135] [Ruby trunk Feature#11653] Add to_proc on Hash
Date: Fri, 24 Jun 2016 08:34:13 +0000	[thread overview]
Message-ID: <redmine.journal-59340.20160624083413.0c203b24e85cb728@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11653.20151104025915@ruby-lang.org

Issue #11653 has been updated by Jörg W Mittag.


Daniel P. Clark wrote:
> Procs can be called the same way a hash is with `[]`.  But a Hash is not mappable as a Proc.
> 
> ~~~ruby
>     my_hash = ->key{{
>       a: 1, b: 2, c: 3, d: 4, e: 5, f: 6
>     }[key]}
> 
>     my_hash[:a]
>     # => 1
> 
>     [:e, :a, :b, :f, :c, :d].map(&my_hash) # hash is now mappable
>     # => [5, 1, 2, 6, 3, 4]
> ~~~
> 
> This seems so straight forward I believe it should be part of the language itself with the `.to_proc` method call.

This is basically a subset of what I proposed a year ago in #11262. I additionally proposed that `Hash` also implement `call`. IMO, it doesn't make much sense to have one without the other: both methods basically say "hey, I'm kinda like a function", and a `Hash` is basically just a function from keys to elements. I proposed the same thing for `Array` and `Set`, which are essentially also just functions from indices to elements (`Array`) or elements to booleans (`Set`). However, so far, there has been no interest in that ticket.

----------------------------------------
Feature #11653: Add to_proc on Hash
https://bugs.ruby-lang.org/issues/11653#change-59340

* Author: Daniel P. Clark
* Status: Closed
* Priority: Normal
* Assignee: Nobuyoshi Nakada
----------------------------------------
Procs can be called the same way a hash is with `[]`.  But a Hash is not mappable as a Proc.

~~~ruby
    my_hash = ->key{{
      a: 1, b: 2, c: 3, d: 4, e: 5, f: 6
    }[key]}

    my_hash[:a]
    # => 1

    [:e, :a, :b, :f, :c, :d].map(&my_hash) # hash is now mappable
    # => [5, 1, 2, 6, 3, 4]
~~~

This seems so straight forward I believe it should be part of the language itself with the `.to_proc` method call.



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2016-06-24  7:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-11653.20151104025915@ruby-lang.org>
2015-11-04  2:59 ` [ruby-core:71327] [Ruby trunk - Feature #11653] [Open] Add to_proc on Hash 6ftdan
2015-11-04  5:24 ` [ruby-core:71329] [Ruby trunk - Feature #11653] " duerst
2015-11-04  6:01 ` [ruby-core:71330] " nobu
2015-11-09  8:03 ` [ruby-core:71404] " matz
2015-11-09 14:32   ` [ruby-core:71416] Unsubscribe Dan Whiteside
2015-11-09  8:08 ` [ruby-core:71405] [Ruby trunk - Feature #11653] Add to_proc on Hash ko1
2016-06-24  8:34 ` Ruby-Lang [this message]
2016-06-25  5:01 ` [ruby-core:76140] [Ruby trunk Feature#11653] " duerst

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-59340.20160624083413.0c203b24e85cb728@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).