Description
A C extension alternative to using flatten.compact or flatten.map. Slight performance improvement (just a constant factor better) than the common idioms, with exactly the same memory performance.
See this blog post for the background and a chart of memory performance.
In addition, collapse makes two other slightly different choices from core Ruby's flatten. 1. Attempting to flatten a recursive Array will error normally. collapse just drops the recursive reference, and continues on its merry way. 2. collapse doesn't accept a level argument. It's all or nothing.
Array#collapse alternatives and similar gems
Based on the "Core Extensions" category.
Alternatively, view Array#collapse alternatives based on common mentions on social networks and blogs.
-
Addressable
Addressable is an alternative implementation to the URI implementation that is part of Ruby's standard library. It is flexible, offers heuristic parsing, and additionally provides extensive support for IRIs and URI templates. -
fast_blank
fast_blank is a simple C extension which provides a fast implementation of Active Support's String#blank? method. -
Finishing Moves
Small, focused, awesome methods added to core Ruby classes. Home of the endlessly useful nil_chain. -
ArrayIncludeMethods
Array#include_all?, Array#include_any?, Array#include_array?, Array#array_index, Array#array_diff_indices, Array#array_intersection_indices, Array#counts, and Array#duplicates operations missing from basic Ruby Array API -
ToCollection
Treat an array of objects and a singular object uniformly as a collection of objects. Especially useful in processing REST Web Service API JSON responses in a functional approach.
CodeRabbit: AI Code Reviews for Developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Array#collapse or a related project?
README
Array#collapse
A C extension alternative to using flatten.compact
or flatten.map
.
Slight performance improvement (just a constant factor better) than the
common idioms, with exactly the same memory performance.
[collapse performance](perf.gif)
See this blog post for the background and a chart of memory performance.
In addition, collapse
makes two other slightly different choices from
core Ruby's flatten
. 1. Attempting to flatten
a recursive Array will
error normally. collapse
just drops the recursive reference, and
continues on its merry way. 2. collapse
doesn't accept
a level
argument. It's all or nothing.
Disclaimer
Should you use this? Needing to compile a native extension for a performance benefit that is not asymptotic is a trade-off I wouldn't always make myself.
Installation
Add this line to your application's Gemfile:
gem 'array_collapse'
And then execute:
$ bundle
Or install it yourself as:
$ gem install array_collapse
Usage
2.2.2 :001 > require 'array_collapse'
=> true
2.2.2 :002 > [1, 2, [3], nil].collapse {|e| e.nil? ? e : e * 2 }
=> [2, 4, 6]
2.2.2 :003 > [1, [2, [3, nil]]].collapse
=> [1, 2, 3]
2.2.2 :004 > a = [1, [2, [3]]]
=> [1, [2, [3]]]
2.2.2 :005 > a << a
=> [1, [2, [3]], [...]]
2.2.2 :006 > a.collapse {|e| e * 3 }
=> [3, 6, 9]
2.2.2 :007 > a.flatten
=> ArgumentError: tried to flatten recursive array
Contributing
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
*Note that all licence references and agreements mentioned in the Array#collapse README section above
are relevant to that project's source code only.