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?
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?).
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.
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.
@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.