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:95794] [Ruby master Feature#16341] Proposal: Set#to_proc
Date: Mon, 11 Nov 2019 15:37:10 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-82615.20191111153710.8a939319540f3104@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16341.20191111150227@ruby-lang.org

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


Well, to be fair, this change is just nice-to-have sugar. I don't expect it to become a thing.

I guess for now the best way to do that is:

```ruby
pets.count { |x| dogs.include?(x) }
# or
pets.count(&dogs.method(:include?))
```

They both are "more clear than value-object-suddenly-becoming-procs". But having implicit conversion would be just a nice feature to make code more compact and expressive (MHO).
Clojure treats sets as functions, btw:

```clojure
(def dogs #{:labrador :husky :bullterrier :corgi})

(count (filter dogs [:parrot :labrador :goldfish :husky :labrador :turtle]))
```

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

* 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-11 15:37 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 ` non.dmitriy [this message]
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 ` [ruby-core:95821] " non.dmitriy
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-82615.20191111153710.8a939319540f3104@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).