Issue #16989 has been updated by Student (Nathan Zook).


marcandre (Marc-Andre Lafortune) wrote in #note-31:
> I'm sorry, I am completely confused by this discussion, I can't make sense of it.
> 
> From what I read:
> 
> ```ruby
> set = {:a, :b, :c} # or whatever notation is used
> set.class # => Hash ???
> set[:a] # => Hash::SetDummy ???
> set[:elem] = :something_else # => what does that mean?
> set.to_a # => [:a, :b, :c, [:elem, :something_else]] ???
> set.transform_values { ... } ???
> set + [1, 2, 3] # => ???
> set + Set[:d, :e] # => ???
> ```
> 
> Am I understanding correctly?
> 
> duerst (Martin Drst) wrote in #note-27:
> > Using `Hash` for sets eliminates the efficiency problem, as we know.
> 
> Are you talking about using `Hash` internally, or having Hash's class and API?
> 
> > Another advantage of using Hash may be that we can get to a syntax that is very close to the actual Mathematical set syntax. We would like to write `{1, 2, 3}`, which may even be possible.
> 
> I don't understand how this might be related in any way to using a Hash? What meaning we decide to give to `{a, b, c}` is completely independent to what `{a: :a, b: :b}` means, or to what `%w{a, b, c}` means, or to `{|a, b| c}` for that matter.
> 
> > At the least, it should be possible to move to the following: `{1 => Hash::DummyValue, 2, 3}`, i.e. if the first key has a special value, then the remaining values can be left out. We may want to choose a better name for `Hash::DummyValue`, maybe something like `Hash::SetDummy`.
> 
> Are you suggesting that instead of writing `Set[1, 2, 3]` we would literally write `{1 => Hash::SetDummy, 2, 3}`?

THIS.

Sets are in no way related to Hashes.  The fact that they are/can be *implemented* as hash keys is convenient, but surfacing such an implementations detail violates programming best practice to a crazy amount.


----------------------------------------
Feature #16989: Sets: need 
https://bugs.ruby-lang.org/issues/16989#change-90558

* Author: marcandre (Marc-Andre Lafortune)
* Status: Assigned
* Priority: Normal
* Assignee: knu (Akinori MUSHA)
----------------------------------------
I am opening a series of feature requests on `Set`, all of them based on this usecase.

The main usecase I have in mind is my recent experience with `RuboCop`. I noticed a big number of frozen arrays being used only to later call `include?` on them. This is `O(n)` instead of `O(1)`.

Trying to convert them to `Set`s causes major compatibility issues, as well as very frustrating situations and some cases that would make them much less efficient.

Because of these incompatibilities, `RuboCop` is in the process of using a custom class based on `Array` with optimized `include?` and `===`. `RuboCop` runs multiple checks on Ruby code. Those checks are called cops. `RuboCop` performance is (IMO) pretty bad and some cops  currently are in `O(n^2)` where n is the size of the code being inspected. Even given these extremely inefficient cops, optimizing the 100+ such arrays (most of which are quite small btw) gave a 5% speed boost.

RuboCop PRs for reference: https://github.com/rubocop-hq/rubocop-ast/pull/29
https://github.com/rubocop-hq/rubocop/pull/8133

My experience tells me that there are many other opportunities to use `Set`s that are missed because `Set`s are not builtin, not known enough and have no shorthand notation.

In this issue I'd like to concentrate the discussion on the following request: `Set`s should be core objects, in the same way that `Complex` were not and are now. Some of the upcoming feature requests would be easier (or only possible) to implement were `Set`s builtin.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>