项目作者: mooreniemi

项目描述 :
:boom: Array#collapse
高级语言: Ruby
项目地址: git://github.com/mooreniemi/array_collapse.git
创建时间: 2016-09-17T15:22:54Z
项目社区:https://github.com/mooreniemi/array_collapse

开源协议:MIT License

下载


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

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:

  1. gem 'array_collapse'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install array_collapse

Usage

  1. 2.2.2 :001 > require 'array_collapse'
  2. => true
  3. 2.2.2 :002 > [1, 2, [3], nil].collapse {|e| e.nil? ? e : e * 2 }
  4. => [2, 4, 6]
  5. 2.2.2 :003 > [1, [2, [3, nil]]].collapse
  6. => [1, 2, 3]
  7. 2.2.2 :004 > a = [1, [2, [3]]]
  8. => [1, [2, [3]]]
  9. 2.2.2 :005 > a << a
  10. => [1, [2, [3]], [...]]
  11. 2.2.2 :006 > a.collapse {|e| e * 3 }
  12. => [3, 6, 9]
  13. 2.2.2 :007 > a.flatten
  14. => 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.