Feature #7226

Add Set#join method as a shortcut for to_a.join

Added by Nathan Broadbent over 2 years ago. Updated 12 months ago.

[ruby-core:48498]
Status:Rejected
Priority:Normal
Assignee:Akinori MUSHA

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.

add_join_to_set.patch Magnifier (788 Bytes) Nathan Broadbent, 10/28/2012 05:38 AM

add_join_to_set.patch Magnifier (756 Bytes) Nathan Broadbent, 10/28/2012 05:45 AM


Related issues

Related to Ruby trunk - Bug #1893: Recursive Enumerable#join is surprising Closed 08/06/2009
Duplicates Ruby trunk - Feature #5970: Add Enumerable#join with same semantics as Array#join Assigned 02/05/2012

History

#1 Updated by Nathan Broadbent over 2 years ago

Sorry, previous patch contained a typo. Here's an updated patch.

#2 Updated by Ayumu AIZAWA over 2 years ago

  • Assignee set to Yukihiro Matsumoto
  • Target version set to next minor

#3 Updated by Rodrigo Rosenfeld Rosas over 2 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 :)

#4 Updated by Yui NARUSE 12 months ago

  • Related to Bug #1893: Recursive Enumerable#join is surprising added

#5 Updated by Nobuyoshi Nakada 12 months 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 Yukihiro Matsumoto to Akinori MUSHA

#6 Updated by Akinori MUSHA 12 months 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.

#7 Updated by Martin Dürst 12 months 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.

#8 Updated by Akinori MUSHA 12 months 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.

Also available in: Atom PDF