ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:95789] [Ruby master Bug#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
@ 2019-11-11 15:02 ` non.dmitriy
  2019-11-11 15:08 ` [ruby-core:95790] [Ruby master Feature#16341] " zverok.offline
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-11 15:02 UTC (permalink / raw)
  To: ruby-core

Issue #16341 has been reported by Nondv (Dmitriy Non).

----------------------------------------
Bug #16341: Proposal: Set#to_proc
https://bugs.ruby-lang.org/issues/16341

* Author: Nondv (Dmitriy Non)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
``` 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95790] [Ruby master Feature#16341] Proposal: Set#to_proc
       [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 ` zverok.offline
  2019-11-11 15:18 ` [ruby-core:95791] " non.dmitriy
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: zverok.offline @ 2019-11-11 15:08 UTC (permalink / raw)
  To: ruby-core

Issue #16341 has been updated by zverok (Victor Shepelev).


Since 2.5, `Set` implements `#===`, so you can just:

```ruby
(1..10).grep_v(banned_numbers)
# => [1, 2, 3, 4, 6, 8, 10] 
```
which is pretty clear and probably more effective than proc conversion.

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95791] [Ruby master Feature#16341] Proposal: Set#to_proc
       [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 ` non.dmitriy
  2019-11-11 15:26 ` [ruby-core:95793] " zverok.offline
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-11 15:18 UTC (permalink / raw)
  To: ruby-core

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


Well, `to_proc` allows to send objects as blocks which can be quite useful not just in case of `select`/`reject`. Also, probably, those two are used more often than `grep`/`grep_v`.

Another example from the top of my head is `count`:

```ruby
dogs = Set[:labrador, :husky, :bullterrier, :corgi]
pets = [:parrot, :labrador, :goldfish, :husky, :labrador, :turtle]
pets.count(&:dogs) # ===> 3
```

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95793] [Ruby master Feature#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-11-11 15:18 ` [ruby-core:95791] " non.dmitriy
@ 2019-11-11 15:26 ` zverok.offline
  2019-11-11 15:37 ` [ruby-core:95794] " non.dmitriy
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: zverok.offline @ 2019-11-11 15:26 UTC (permalink / raw)
  To: ruby-core

Issue #16341 has been updated by zverok (Victor Shepelev).


Fair enough. ...well, you still can 
```ruby
pets.count(&dogs.:include?)
```
until the core team haven't reverted it :))))

(Which, for me, is more clear than value-objects-suddenly-becoming-procs, but apparently it is only me)

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95794] [Ruby master Feature#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-11-11 15:26 ` [ruby-core:95793] " zverok.offline
@ 2019-11-11 15:37 ` non.dmitriy
  2019-11-11 23:43 ` [ruby-core:95804] " shevegen
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-11 15:37 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95804] [Ruby master Feature#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-11-11 15:37 ` [ruby-core:95794] " non.dmitriy
@ 2019-11-11 23:43 ` shevegen
  2019-11-12 18:58 ` [ruby-core:95819] " non.dmitriy
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: shevegen @ 2019-11-11 23:43 UTC (permalink / raw)
  To: ruby-core

Issue #16341 has been updated by shevegen (Robert A. Heiler).


> this change is just nice-to-have sugar. I don't expect it to
> become a thing.

The ruby core team often points out that having good use cases may
help a proposal; and of course avoiding other problems such as
backwards-incompatibility or such.

Your initial comment is quite sparse, so zverok sort of got you to
explain more lateron. ;)

I am not really using ruby in a functional-centric manner nor do I
know clojure (aside from superficial glances), but to me personally
I am not completely sure if the use case has been explained. Unless
it was only syntactic sugar of course.

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95819] [Ruby master Feature#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-11-11 23:43 ` [ruby-core:95804] " shevegen
@ 2019-11-12 18:58 ` non.dmitriy
  2019-11-12 19:05 ` [ruby-core:95820] " non.dmitriy
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-12 18:58 UTC (permalink / raw)
  To: ruby-core

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


This *is* a syntactic sugar. Using `&` + `to_proc` in this case is the same (not technically, but algorithmically, I guess) as providing an explicit block `something.some_method { |x| some_set.include?(x) }`

I don't find it crucial in any way and, to be honest, I don't really use sets that much (I prefer using hashes directly). But this feature could make some more a tiny bit easier to read from English language perspective (I *think*)

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95820] [Ruby master Feature#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-11-12 18:58 ` [ruby-core:95819] " non.dmitriy
@ 2019-11-12 19:05 ` non.dmitriy
  2019-11-12 19:09 ` [ruby-core:95821] " non.dmitriy
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-12 19:05 UTC (permalink / raw)
  To: ruby-core

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


Speaking of hashes, they could implement implicit proc conversion as well:

```ruby
class Hash
  def to_proc
    ->(key) { self[key] }
  end
end

dogs = ['Lucky', 'Trimp', 'Lady']
favourite_food = { 'Lucky' => 'salmon', 'Trimp' => 'pasta', 'Lady' => 'pasta' }

food_to_order = dogs.map(&favourite_food)
```


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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95821] [Ruby master Feature#16341] Proposal: Set#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2019-11-12 19:05 ` [ruby-core:95820] " non.dmitriy
@ 2019-11-12 19:09 ` 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
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-12 19:09 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95822] [Ruby master Feature#16341] Proposal: Set#to_proc and Hash#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2019-11-12 19:09 ` [ruby-core:95821] " non.dmitriy
@ 2019-11-12 19:44 ` shannonskipper
  2019-11-12 20:12 ` [ruby-core:95823] " non.dmitriy
  10 siblings, 0 replies; 11+ messages in thread
From: shannonskipper @ 2019-11-12 19:44 UTC (permalink / raw)
  To: ruby-core

Issue #16341 has been updated by shan (Shannon Skipper).


Nondv (Dmitry Non) wrote:
> Speaking of hashes, they could implement implicit proc conversion as well:
> 
> ```ruby
> class Hash
>   def to_proc
>     ->(key) { self[key] }
>   end
> end
> 
> dogs = ['Lucky', 'Tramp', 'Lady']
> favourite_food = { 'Lucky' => 'salmon', 'Tramp' => 'pasta', 'Lady' => 'pasta' }
> 
> food_to_order = dogs.map(&favourite_food)
> ```

This already works! Hash#to_proc was added in Ruby 2.3. [ruby-core:11653](https://bugs.ruby-lang.org/issues/11653)

I like the idea of Set#to_proc.

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

* 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]
```

**UPD**

also for hash:

```ruby
class Hash
  def to_proc
    ->(key) { self[key] }
  end
end

dogs = ['Lucky', 'Tramp', 'Lady']
favourite_food = { 'Lucky' => 'salmon', 'Tramp' => 'pasta', 'Lady' => 'pasta' }

food_to_order = dogs.map(&favourite_food)
```



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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:95823] [Ruby master Feature#16341] Proposal: Set#to_proc and Hash#to_proc
       [not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
                   ` (9 preceding siblings ...)
  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 ` non.dmitriy
  10 siblings, 0 replies; 11+ messages in thread
From: non.dmitriy @ 2019-11-12 20:12 UTC (permalink / raw)
  To: ruby-core

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


shan (Shannon Skipper) wrote:

> This already works!

I can't believe I'm so oblivious :D

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

* 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]
```

**UPD**

also for hash:

```ruby
class Hash
  def to_proc
    ->(key) { self[key] }
  end
end

dogs = ['Lucky', 'Tramp', 'Lady']
favourite_food = { 'Lucky' => 'salmon', 'Tramp' => 'pasta', 'Lady' => 'pasta' }

food_to_order = dogs.map(&favourite_food)
```



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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-11-12 20:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [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

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).