ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: non.dmitriy@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95821] [Ruby master Feature#16341] Proposal: Set#to_proc
Date: Tue, 12 Nov 2019 19:09:28 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-82648.20191112190927.ebf99a01ca564d07@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16341.20191111150227@ruby-lang.org

Issue #16341 has been updated by Nondv (Dmitry Non).


The main problem is that implicit conversion can be confusing, especially, if it's not obvious what the resulting proc is going to do.

However, I think that hashes are being used *mainly* for making key-value pairs and accessing them and sets are being used for checking if something is included.
So usage of `:[]` and `:include?` seems appropriate and relatively straight-forward to me.

Of course, depending on the context. With map/reduce/count it does make sense indeed but maybe there're cases when it can make things hard to understand

----------------------------------------
Feature #16341: Proposal: Set#to_proc
https://bugs.ruby-lang.org/issues/16341#change-82648

* Author: Nondv (Dmitry Non)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
``` ruby
class Set
  def to_proc
    -> (x) { include?(x) } # or method(:include?).to_proc
  end
end
```

Usage:

```ruby
require 'set'

banned_numbers = Set[0, 5, 7, 9]
(1..10).reject(&banned_numbers) # ===> [1, 2, 3, 4, 6, 8, 10]
```



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

  parent reply	other threads:[~2019-11-12 19:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
2019-11-11 15:02 ` [ruby-core:95789] [Ruby master Bug#16341] Proposal: Set#to_proc non.dmitriy
2019-11-11 15:08 ` [ruby-core:95790] [Ruby master Feature#16341] " zverok.offline
2019-11-11 15:18 ` [ruby-core:95791] " non.dmitriy
2019-11-11 15:26 ` [ruby-core:95793] " zverok.offline
2019-11-11 15:37 ` [ruby-core:95794] " non.dmitriy
2019-11-11 23:43 ` [ruby-core:95804] " shevegen
2019-11-12 18:58 ` [ruby-core:95819] " non.dmitriy
2019-11-12 19:05 ` [ruby-core:95820] " non.dmitriy
2019-11-12 19:09 ` non.dmitriy [this message]
2019-11-12 19:44 ` [ruby-core:95822] [Ruby master Feature#16341] Proposal: Set#to_proc and Hash#to_proc shannonskipper
2019-11-12 20:12 ` [ruby-core:95823] " non.dmitriy

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-82648.20191112190927.ebf99a01ca564d07@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).