Project

General

Profile

Actions

Feature #15123

closed

Enumerable#compact proposal

Added by printercu (Max Melentiev) about 6 years ago. Updated about 5 years ago.

Status:
Feedback
Target version:
-
[ruby-core:89017]

Description

Hi!

While Enumerable does not provide #compact method, it requires changing code in some cases to substitute array with enumerator.

For example, to reduce memory usage it's usual to change large_array.map { to_heavy_object }.chained_methods to large_array.lazy.... However if chained_methods contains compact, this change will fail. Replacing compact with reject(&:nil?) fixes it.

What do you think about adding #compact to Enumerable?

Updated by greggzst (Grzegorz Jakubiak) about 6 years ago

I'm in favor of this proposal. It simplifies working with large and small collections so one doesn't have to remember that can't use #compact when enumerator is returned and have to fall back to #reject(:nil?).

Updated by marcandre (Marc-Andre Lafortune) about 6 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

Proposal seems acceptable to me.

Just to be clear: I imagine that Lazy#compact would still be lazy. Also compact is roughly select(&:itself), not reject(&:nil?) which would wrongly keep false.

Updated by Eregon (Benoit Daloze) about 6 years ago

marcandre (Marc-Andre Lafortune) wrote:

Also compact is roughly select(&:itself), not reject(&:nil?) which would wrongly keep false.

No, #compact only removes nil: ["a" ,false ,nil].compact => ["a", false].

Updated by shevegen (Robert A. Heiler) about 6 years ago

I think if the meaning is consistent (e. g. .compact meaning to eliminate
nil values from a given collection) then this seems ok. Perhaps this could
be added for the next developer meeting.

Updated by marcandre (Marc-Andre Lafortune) about 6 years ago

Eregon (Benoit Daloze) wrote:

No, #compact only removes nil: ["a" ,false ,nil].compact => ["a", false].

Lol, ouch, not sure how I could be so confused when I wrote that, sorry!

Actions #6

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

  • Tracker changed from Bug to Feature
  • Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN)

Updated by shyouhei (Shyouhei Urabe) over 5 years ago

Just leaving a comment that I wanted this method in my pet project just now. So +1.

Updated by matz (Yukihiro Matsumoto) about 5 years ago

I don't see enough demand for compact where we have reject(&:nil?). Any additional use-case?

Matz.

Actions #9

Updated by ko1 (Koichi Sasada) about 5 years ago

  • Status changed from Open to Feedback

Updated by baweaver (Brandon Weaver) about 5 years ago

@matz (Yukihiro Matsumoto): Its presence in Array and Hash make it more of a common interface that I could see being defined for Enumerable in general, though the immediate usecases are around Lazy. As mentioned above, sometimes one wants to use Enumerators directly or returns one from an Enumerable method which can cause some conflicts of available methods.

I believe it could be considered surprising that compact does not necessarily work with all collection-like types.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0