Feature #7226
closedAdd Set#join method as a shortcut for to_a.join
Description
I was surprised that Set.new.join
gives me a NoMethodError.
This patch that adds a #join method to Set, which is a shortcut for to_a.join
.
Files
Updated by nathan.f77 (Nathan Broadbent) about 12 years ago
- File add_join_to_set.patch add_join_to_set.patch added
Sorry, previous patch contained a typo. Here's an updated patch.
Updated by ayumin (Ayumu AIZAWA) about 12 years ago
- Assignee set to matz (Yukihiro Matsumoto)
- Target version set to 2.6
Updated by rosenfeld (Rodrigo Rosenfeld Rosas) about 12 years ago
+1. I was about to create the same feature request some weeks ago when I tried to join a set but was too lazy to do so :)
Updated by naruse (Yui NARUSE) over 10 years ago
- Related to Bug #1893: Recursive Enumerable#join is surprising added
Updated by nobu (Nobuyoshi Nakada) over 10 years ago
- Subject changed from Added #join method as a shortcut for to_a.join to Add Set#join method as a shortcut for to_a.join
- Status changed from Open to Assigned
- Assignee changed from matz (Yukihiro Matsumoto) to knu (Akinori MUSHA)
Updated by knu (Akinori MUSHA) over 10 years ago
- Status changed from Assigned to Rejected
The proposed implementation is far from efficient, collecting all elements into a temporary array only for calling Array#join.
It wouldn't be worth having it unless it went something like this: inject(nil) { |s, o| s.nil? ? "#{o}" : s << "#{sep}#{o}" } || ''
In any case, the method would better be implemented in Enumerable rather than in Set, because it would not be specific to Set at all but apply to any class that implements each, and Set is an unordered collection you shouldn't expect to have #join in the first place.
Updated by duerst (Martin Dürst) over 10 years ago
Akinori MUSHA wrote:
The proposed implementation is far from efficient, collecting all elements into a temporary array only for calling Array#join.
It wouldn't be worth having it unless it went something like this: inject(nil) { |s, o| s.nil? ? "#{o}" : s << "#{sep}#{o}" } || ''
I tried some very simple cases, and didn't see much of a difference. It's often better to start with a simple implementation and make it more complicated if additional performance is really needed.
In any case, the method would better be implemented in Enumerable rather than in Set,
I agree.
Updated by knu (Akinori MUSHA) over 10 years ago
Martin Dürst wrote:
I tried some very simple cases, and didn't see much of a difference. It's often better to start with a simple implementation and make it more complicated if additional performance is really needed.
If .to_a.join is what you want then you can and should live with it.
I implied by the phrase "far from efficient" that it would be against our expectation for #join to consume unnecessary time and space. What .to_a.join would do is to iterate to accumulate to iterate to accumulate, which certainly involves unnecessary complexity.
In any case, the method would better be implemented in Enumerable rather than in Set,
I agree.
Enumerable represents a stream, #join is a stream friendly operation by nature, and I believe no .to_a should take place there.
With all being said, Enumerable#join has a history of once being added and then removed later as seen in #1893.
We must work out a way to add it again, resolving the compatibility issue with Array#join somehow.