[ruby-core:124289] [Ruby Feature#21791] Implement Set#compact/Set#compact!, these should return Set instead of Array
Issue #21791 has been reported by herwin (Herwin W). ---------------------------------------- Feature #21791: Implement Set#compact/Set#compact!, these should return Set instead of Array https://bugs.ruby-lang.org/issues/21791 * Author: herwin (Herwin W) * Status: Open ---------------------------------------- I recently had to remove a nil value from a Set, and ended up with an Array: ``` irb(main):001> Set[1, 2, nil, 3].compact => [1, 2, 3] irb(main):002> Set[1, 2, nil, 3].compact.class => Array ``` Since there is no dedicated `Set#compact`, this is done via `Enumerable#compact` and this results in an Array. To preserve the Set, the following works: ```ruby set - [nil] # compact set.delete_if(&:nil?) # compact! set.compact.to_set # compact, but slow ``` Both are rather ugly. This patch implements `Set#compact` and `Set#compact!` in a way that preserves the class. There are probably more methods that could have their own implementation, for example `Set#select`/`Set#reject` now returns arrays too (but `Set#select!` and `Set#reject!` work as expected. Pull request: https://github.com/ruby/ruby/pull/15614 -- https://bugs.ruby-lang.org/
Issue #21791 has been updated by herwin (Herwin W). I wrote the rubyspecs for this change as well, they now check for version 4.0, but since we're very late into the 4.0 release cycle, it might be better to bump this to 4.1. ---------------------------------------- Feature #21791: Implement Set#compact/Set#compact!, these should return Set instead of Array https://bugs.ruby-lang.org/issues/21791#change-115788 * Author: herwin (Herwin W) * Status: Open ---------------------------------------- I recently had to remove a nil value from a Set, and ended up with an Array: ``` irb(main):001> Set[1, 2, nil, 3].compact => [1, 2, 3] irb(main):002> Set[1, 2, nil, 3].compact.class => Array ``` Since there is no dedicated `Set#compact`, this is done via `Enumerable#compact` and this results in an Array. To preserve the Set, the following works: ```ruby set - [nil] # compact set.delete_if(&:nil?) # compact! set.compact.to_set # compact, but slow ``` Both are rather ugly. This patch implements `Set#compact` and `Set#compact!` in a way that preserves the class. There are probably more methods that could have their own implementation, for example `Set#select`/`Set#reject` now returns arrays too (but `Set#select!` and `Set#reject!` work as expected. Pull request: https://github.com/ruby/ruby/pull/15614 -- https://bugs.ruby-lang.org/
Issue #21791 has been updated by byroot (Jean Boussier). Since `Set#delete` is O(1) performance and return self, why not just: ```ruby
Set[1, 2, nil, 3].delete(nil) => Set[1, 2, 3]
----------------------------------------
Feature #21791: Implement Set#compact/Set#compact!, these should return Set instead of Array
https://bugs.ruby-lang.org/issues/21791#change-115817
* Author: herwin (Herwin W)
* Status: Open
----------------------------------------
I recently had to remove a nil value from a Set, and ended up with an Array:
irb(main):001> Set[1, 2, nil, 3].compact => [1, 2, 3] irb(main):002> Set[1, 2, nil, 3].compact.class => Array ``` Since there is no dedicated `Set#compact`, this is done via `Enumerable#compact` and this results in an Array. To preserve the Set, the following works: ```ruby set - [nil] # compact set.delete_if(&:nil?) # compact! set.compact.to_set # compact, but slow ``` Both are rather ugly. This patch implements `Set#compact` and `Set#compact!` in a way that preserves the class. There are probably more methods that could have their own implementation, for example `Set#select`/`Set#reject` now returns arrays too (but `Set#select!` and `Set#reject!` work as expected. Pull request: https://github.com/ruby/ruby/pull/15614 -- https://bugs.ruby-lang.org/
participants (2)
-
byroot (Jean Boussier) -
herwin (Herwin W)