array_collapse 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +45 -0
- data/ext/array_collapse/array_collapse.c +47 -0
- data/ext/array_collapse/extconf.rb +3 -0
- data/lib/.gemkeep +0 -0
- data/spec/array_collapse_spec.rb +21 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 087a4d086fa6b69926231193c797f4f065405f2a
|
4
|
+
data.tar.gz: 555e9172fa51385cf739a5197e57956866991060
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e58275c48bd89224d59d4486f2bea1dfbaa7eca69fff407c4e67fd2ea82aec70d9135b8a7b0bd39e56b3b701595181160ded9a99e1b561684fcc604f776143c
|
7
|
+
data.tar.gz: 8d02a79814e045348a0dac515fc2a653b8c0271e7f3f57771a727ec11c2f7084e089cdef4beddea41c0b46e06fc8a1f4ebb9ef93b9d8968c986843017a193854
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alex Moore-Niemi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Array#collapse
|
2
|
+
|
3
|
+
A C extension alternative to using `flatten.compact` or `flatten.map`.
|
4
|
+
Slight performance improvement (just a constant factor better) than the
|
5
|
+
common idioms, with exactly the same memory performance.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'array_collapse'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install array_collapse
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
2.2.2 :001 > require 'array_collapse'
|
27
|
+
=> true
|
28
|
+
2.2.2 :002 > [1, 2, [3], nil].collapse {|e| e.nil? ? e : e * 2 }
|
29
|
+
=> [2, 4, 6]
|
30
|
+
2.2.2 :003 > [1, [2, [3, nil]]].collapse
|
31
|
+
=> [1, 2, 3]
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at
|
37
|
+
https://github.com/mooreniemi/array_collapse. This project is intended to
|
38
|
+
be a safe, welcoming space for collaboration, and contributors are
|
39
|
+
expected to adhere to the [Contributor
|
40
|
+
Covenant](http://contributor-covenant.org) code of conduct.
|
41
|
+
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
void Init_array_collapse();
|
4
|
+
|
5
|
+
VALUE method_collapse(VALUE self, VALUE args) {
|
6
|
+
long len = RARRAY_LEN(self);
|
7
|
+
VALUE result = rb_ary_new2(len);
|
8
|
+
VALUE stack = rb_ary_new();
|
9
|
+
|
10
|
+
#ifdef LOGGING
|
11
|
+
FILE *f = fopen("clog.txt", "w");
|
12
|
+
if (f == NULL) {
|
13
|
+
printf("no log file found\n");
|
14
|
+
exit(1);
|
15
|
+
}
|
16
|
+
fprintf(f, "len: %lu\n", len);
|
17
|
+
fprintf(f, "array: %s\n", RSTRING_PTR(rb_ary_to_s(self)));
|
18
|
+
#endif
|
19
|
+
|
20
|
+
rb_ary_push(stack,self);
|
21
|
+
while(RARRAY_LEN(stack) > 0) {
|
22
|
+
VALUE array = rb_ary_pop(stack);
|
23
|
+
long i;
|
24
|
+
for(i = 0; i < RARRAY_LEN(array); i++) {
|
25
|
+
VALUE e = RARRAY_AREF(array,i);
|
26
|
+
switch (TYPE(e)) {
|
27
|
+
case T_ARRAY:
|
28
|
+
rb_ary_push(stack, e);
|
29
|
+
break;
|
30
|
+
default:
|
31
|
+
if (rb_block_given_p()) {
|
32
|
+
e = rb_yield(e);
|
33
|
+
}
|
34
|
+
if (!NIL_P(e)) {
|
35
|
+
rb_ary_push(result, e);
|
36
|
+
}
|
37
|
+
break;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
return result;
|
43
|
+
}
|
44
|
+
|
45
|
+
void Init_array_collapse() {
|
46
|
+
rb_define_method(rb_cArray, "collapse", method_collapse, -2);
|
47
|
+
}
|
data/lib/.gemkeep
ADDED
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'array_collapse'
|
2
|
+
|
3
|
+
RSpec.describe 'Array#collapse' do
|
4
|
+
it 'lives on Array' do
|
5
|
+
expect([1].respond_to?(:collapse)).to eq(true)
|
6
|
+
end
|
7
|
+
it 'applies a block to every element' do
|
8
|
+
expect([1, 2].collapse { |e| e * 2 }).to eq([2, 4])
|
9
|
+
end
|
10
|
+
it 'flattens a nested array' do
|
11
|
+
expect([1, [2]].collapse).to eq([1, 2])
|
12
|
+
end
|
13
|
+
it 'flattens and maps' do
|
14
|
+
expect([1, [2, [3]]].collapse { |e| e * 2 }).to eq([2, 4, 6])
|
15
|
+
end
|
16
|
+
it 'drops nils' do
|
17
|
+
expect([nil, 1, [2]].collapse).to eq([1, 2])
|
18
|
+
expect([1, nil, [2, [3, nil]]].collapse { |e| e.nil? ? e : e * 2 }).
|
19
|
+
to eq([2, 4, 6])
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array_collapse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Moore-Niemi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: 'Like #flatten, but rejecting nils and allowing a block.'
|
70
|
+
email:
|
71
|
+
- moore.niemi@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions:
|
74
|
+
- ext/array_collapse/extconf.rb
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- ext/array_collapse/array_collapse.c
|
80
|
+
- ext/array_collapse/extconf.rb
|
81
|
+
- lib/.gemkeep
|
82
|
+
- spec/array_collapse_spec.rb
|
83
|
+
homepage: https://github.com/mooreniemi/array-collapse
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.4.5.1
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Array#collapse
|
107
|
+
test_files:
|
108
|
+
- spec/array_collapse_spec.rb
|