Project

General

Profile

Actions

Feature #7226

closed

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

Added by nathan.f77 (Nathan Broadbent) over 11 years ago. Updated almost 10 years ago.

Status:
Rejected
Target version:
[ruby-core:48498]

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

add_join_to_set.patch (788 Bytes) add_join_to_set.patch nathan.f77 (Nathan Broadbent), 10/28/2012 05:38 AM
add_join_to_set.patch (756 Bytes) add_join_to_set.patch nathan.f77 (Nathan Broadbent), 10/28/2012 05:45 AM

Related issues 2 (1 open1 closed)

Related to Ruby master - Bug #1893: Recursive Enumerable#join is surprisingClosedmatz (Yukihiro Matsumoto)08/06/2009Actions
Is duplicate of Ruby master - Feature #5970: Add Enumerable#join with same semantics as Array#joinAssignedmatz (Yukihiro Matsumoto)Actions

Updated by nathan.f77 (Nathan Broadbent) over 11 years ago

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

Updated by ayumin (Ayumu AIZAWA) over 11 years ago

  • Assignee set to matz (Yukihiro Matsumoto)
  • Target version set to 2.6

Updated by rosenfeld (Rodrigo Rosenfeld Rosas) over 11 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) almost 10 years ago

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

Updated by nobu (Nobuyoshi Nakada) almost 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) almost 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) almost 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) almost 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0