enumerating 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/README.markdown +8 -0
- data/lib/enumerating/concatenating.rb +28 -0
- data/lib/enumerating/filtering.rb +39 -0
- data/lib/enumerating/mixing.rb +1 -0
- data/lib/enumerating/version.rb +1 -1
- data/spec/enumerating/concatenating_spec.rb +21 -0
- data/spec/enumerating/filtering_spec.rb +56 -0
- data/spec/enumerating/zipping_spec.rb +1 -1
- metadata +6 -2
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -10,6 +10,7 @@ Enumerating extends Enumerable with "lazy" versions of various common operations
|
|
10
10
|
* `#rejecting` selects elements that fail a test block (like `Enumerable#reject`)
|
11
11
|
* `#collecting` applies a transforming block to each element (like `Enumerable#collect`)
|
12
12
|
* `#uniqing` discards duplicates (like `Enumerable#uniq`)
|
13
|
+
* `#taking`, `#taking_while`, `#dropping` and `#dropping_while` also do what you expect
|
13
14
|
|
14
15
|
We say the "...ing" variants are "lazy", because they defer per-element processing until the result is used. They return Enumerable "result proxy" objects, rather than Arrays, and only perform the actual filtering (or transformation) as the result proxy is enumerated.
|
15
16
|
|
@@ -70,6 +71,13 @@ Enumerating also provides some interesting ways to combine several Enumerable co
|
|
70
71
|
array2 = [2,4,7]
|
71
72
|
Enumerating.zipping(array1, array2) # generates: [1,2], [3,4], [6,7]
|
72
73
|
|
74
|
+
`Enumerating.concatenating` concatenates collections.
|
75
|
+
|
76
|
+
array1 = [1,3,6]
|
77
|
+
array2 = [2,4,7]
|
78
|
+
Enumerating.concatenating(array1, array2)
|
79
|
+
# generates: [1,3,6,2,4,7]
|
80
|
+
|
73
81
|
`Enumerating.merging` merges multiple collections, preserving sort-order. The inputs are assumed to be sorted already.
|
74
82
|
|
75
83
|
array1 = [1,4,5]
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
module Enumerating
|
3
|
+
|
4
|
+
class Concatenator
|
5
|
+
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(enumerables)
|
9
|
+
@enumerables = enumerables
|
10
|
+
end
|
11
|
+
|
12
|
+
def each(&block)
|
13
|
+
@enumerables.each do |enumerable|
|
14
|
+
enumerable.each(&block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
class << Enumerating
|
23
|
+
|
24
|
+
def concatenating(*enumerables)
|
25
|
+
Enumerating::Concatenator.new(enumerables)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -64,5 +64,44 @@ module Enumerable
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
def taking(n)
|
68
|
+
Enumerating::Filter.new do |output|
|
69
|
+
if n > 0
|
70
|
+
each_with_index do |element, index|
|
71
|
+
output.call(element)
|
72
|
+
break if index + 1 == n
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def taking_while
|
79
|
+
Enumerating::Filter.new do |output|
|
80
|
+
each do |element|
|
81
|
+
break unless yield(element)
|
82
|
+
output.call(element)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def dropping(n)
|
88
|
+
Enumerating::Filter.new do |output|
|
89
|
+
each_with_index do |element, index|
|
90
|
+
next if index < n
|
91
|
+
output.call(element)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def dropping_while
|
97
|
+
Enumerating::Filter.new do |output|
|
98
|
+
taking = false
|
99
|
+
each do |element|
|
100
|
+
taking ||= !yield(element)
|
101
|
+
output.call(element) if taking
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
67
106
|
end
|
68
107
|
|
data/lib/enumerating/mixing.rb
CHANGED
data/lib/enumerating/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Enumerating do
|
4
|
+
|
5
|
+
describe ".concatenating" do
|
6
|
+
|
7
|
+
it "concatenates multiple Enumerables" do
|
8
|
+
@array1 = [1,5,3]
|
9
|
+
@array2 = [2,9,4]
|
10
|
+
@zip = Enumerating.concatenating(@array1, @array2)
|
11
|
+
@zip.to_a.should == [1,5,3,2,9,4]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "is lazy" do
|
15
|
+
@zip = Enumerating.concatenating([3,4], [1,2].with_time_bomb)
|
16
|
+
@zip.take(3).should == [3,4,1]
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -70,5 +70,61 @@ describe Enumerable do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
end
|
73
|
+
|
74
|
+
describe "#taking" do
|
75
|
+
|
76
|
+
it "includes the specified number" do
|
77
|
+
@array = [1,2,3,4]
|
78
|
+
@array.taking(3).to_a.should == [1,2,3]
|
79
|
+
end
|
80
|
+
|
81
|
+
it "is lazy" do
|
82
|
+
[1,2].with_time_bomb.taking(2).to_a.should == [1,2]
|
83
|
+
end
|
84
|
+
|
85
|
+
it "copes with 0" do
|
86
|
+
[].with_time_bomb.taking(0).to_a.should == []
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#taking_while" do
|
92
|
+
|
93
|
+
it "takes elements as long as the predicate is true" do
|
94
|
+
@array = [2,4,6,3]
|
95
|
+
@array.taking_while(&:even?).to_a.should == [2,4,6]
|
96
|
+
end
|
97
|
+
|
98
|
+
it "is lazy" do
|
99
|
+
[2,3].with_time_bomb.taking_while(&:even?).to_a.should == [2]
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "#dropping" do
|
105
|
+
|
106
|
+
it "excludes the specified number" do
|
107
|
+
@array = [1,2,3,4]
|
108
|
+
@array.dropping(2).to_a.should == [3,4]
|
109
|
+
end
|
110
|
+
|
111
|
+
it "is lazy" do
|
112
|
+
[1,2,3,4].with_time_bomb.dropping(2).taking(1).to_a.should == [3]
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "#dropping_while" do
|
118
|
+
|
119
|
+
it "drops elements as long as the predicate is true" do
|
120
|
+
@array = [2,4,6,3,4]
|
121
|
+
@array.dropping_while(&:even?).to_a.should == [3,4]
|
122
|
+
end
|
123
|
+
|
124
|
+
it "is lazy" do
|
125
|
+
[2,3].with_time_bomb.dropping_while(&:even?).taking(1).to_a.should == [3]
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
73
129
|
|
74
130
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: enumerating
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mike Williams
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-26 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Enumerating extends Enumerable with "lazy" versions of various operations, allowing streamed processing of large (or even infinite) collections. Even in Ruby 1.8.x.
|
@@ -29,11 +29,13 @@ files:
|
|
29
29
|
- benchmarks/pipeline_bench.rb
|
30
30
|
- enumerating.gemspec
|
31
31
|
- lib/enumerating.rb
|
32
|
+
- lib/enumerating/concatenating.rb
|
32
33
|
- lib/enumerating/filtering.rb
|
33
34
|
- lib/enumerating/merging.rb
|
34
35
|
- lib/enumerating/mixing.rb
|
35
36
|
- lib/enumerating/version.rb
|
36
37
|
- lib/enumerating/zipping.rb
|
38
|
+
- spec/enumerating/concatenating_spec.rb
|
37
39
|
- spec/enumerating/filtering_spec.rb
|
38
40
|
- spec/enumerating/merging_spec.rb
|
39
41
|
- spec/enumerating/zipping_spec.rb
|
@@ -66,7 +68,9 @@ signing_key:
|
|
66
68
|
specification_version: 3
|
67
69
|
summary: Lazy filtering/transforming of Enumerable collections
|
68
70
|
test_files:
|
71
|
+
- spec/enumerating/concatenating_spec.rb
|
69
72
|
- spec/enumerating/filtering_spec.rb
|
70
73
|
- spec/enumerating/merging_spec.rb
|
71
74
|
- spec/enumerating/zipping_spec.rb
|
72
75
|
- spec/spec_helper.rb
|
76
|
+
has_rdoc:
|