lazily 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +0 -4
- data/lib/lazily/filtering.rb +33 -28
- data/lib/lazily/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -134,10 +134,6 @@ Q: Why use Lazily, when Ruby 2.x has built-in lazy enumerations?
|
|
134
134
|
- Features: Lazily provides some extra features not present in Ruby 2.0, such as multi-threaded lazy enumeration.
|
135
135
|
- Speed: Despite being implemented in pure Ruby, `Enumerable#lazily` actually performs a little better than `Enumerable#lazy`.
|
136
136
|
|
137
|
-
Q: When would you use built-in Ruby lazy enumerations, rather than Lazily?
|
138
|
-
|
139
|
-
- I wouldn't.
|
140
|
-
|
141
137
|
### Others
|
142
138
|
|
143
139
|
See also:
|
data/lib/lazily/filtering.rb
CHANGED
@@ -10,9 +10,9 @@ module Lazily
|
|
10
10
|
# @see ::Enumerable#collect
|
11
11
|
#
|
12
12
|
def collect(&transformation)
|
13
|
-
filter("collect") do |
|
13
|
+
filter("collect") do |yielder|
|
14
14
|
each do |element|
|
15
|
-
|
15
|
+
yielder.call yield(element)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -26,9 +26,9 @@ module Lazily
|
|
26
26
|
# @see ::Enumerable#select
|
27
27
|
#
|
28
28
|
def select(&predicate)
|
29
|
-
filter("select") do |
|
29
|
+
filter("select") do |yielder|
|
30
30
|
each do |element|
|
31
|
-
|
31
|
+
yielder.call(element) if yield(element)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
@@ -44,9 +44,9 @@ module Lazily
|
|
44
44
|
# @see ::Enumerable#reject
|
45
45
|
#
|
46
46
|
def reject
|
47
|
-
filter("reject") do |
|
47
|
+
filter("reject") do |yielder|
|
48
48
|
each do |element|
|
49
|
-
|
49
|
+
yielder.call(element) unless yield(element)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -61,7 +61,7 @@ module Lazily
|
|
61
61
|
# @see ::Enumerable#uniq
|
62
62
|
#
|
63
63
|
def uniq
|
64
|
-
filter("uniq") do |
|
64
|
+
filter("uniq") do |yielder|
|
65
65
|
seen = Set.new
|
66
66
|
each do |element|
|
67
67
|
key = if block_given?
|
@@ -69,7 +69,7 @@ module Lazily
|
|
69
69
|
else
|
70
70
|
element
|
71
71
|
end
|
72
|
-
|
72
|
+
yielder.call(element) if seen.add?(key)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -82,11 +82,11 @@ module Lazily
|
|
82
82
|
# @see ::Enumerable#take
|
83
83
|
#
|
84
84
|
def take(n)
|
85
|
-
filter("take") do |
|
85
|
+
filter("take") do |yielder, all_done|
|
86
86
|
if n > 0
|
87
87
|
each_with_index do |element, index|
|
88
|
-
|
89
|
-
throw
|
88
|
+
yielder.call(element)
|
89
|
+
throw all_done if index + 1 == n
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
@@ -99,10 +99,10 @@ module Lazily
|
|
99
99
|
# @see ::Enumerable#take_while
|
100
100
|
#
|
101
101
|
def take_while(&predicate)
|
102
|
-
filter("take_while") do |
|
102
|
+
filter("take_while") do |yielder, all_done|
|
103
103
|
each do |element|
|
104
|
-
throw
|
105
|
-
|
104
|
+
throw all_done unless yield(element)
|
105
|
+
yielder.call(element)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
end
|
@@ -115,10 +115,10 @@ module Lazily
|
|
115
115
|
# @see ::Enumerable#drop
|
116
116
|
#
|
117
117
|
def drop(n)
|
118
|
-
filter("drop") do |
|
118
|
+
filter("drop") do |yielder|
|
119
119
|
each_with_index do |element, index|
|
120
120
|
next if index < n
|
121
|
-
|
121
|
+
yielder.call(element)
|
122
122
|
end
|
123
123
|
end
|
124
124
|
end
|
@@ -130,11 +130,11 @@ module Lazily
|
|
130
130
|
# @see ::Enumerable#drop_while
|
131
131
|
#
|
132
132
|
def drop_while(&predicate)
|
133
|
-
filter("drop_while") do |
|
133
|
+
filter("drop_while") do |yielder|
|
134
134
|
take = false
|
135
135
|
each do |element|
|
136
136
|
take ||= !yield(element)
|
137
|
-
|
137
|
+
yielder.call(element) if take
|
138
138
|
end
|
139
139
|
end
|
140
140
|
end
|
@@ -146,7 +146,7 @@ module Lazily
|
|
146
146
|
# @see ::Enumerable#grep
|
147
147
|
#
|
148
148
|
def grep(pattern)
|
149
|
-
filter("grep") do |
|
149
|
+
filter("grep") do |yielder|
|
150
150
|
each do |element|
|
151
151
|
if pattern === element
|
152
152
|
result = if block_given?
|
@@ -154,7 +154,7 @@ module Lazily
|
|
154
154
|
else
|
155
155
|
element
|
156
156
|
end
|
157
|
-
|
157
|
+
yielder.call(result)
|
158
158
|
end
|
159
159
|
end
|
160
160
|
end
|
@@ -167,12 +167,12 @@ module Lazily
|
|
167
167
|
# @see ::Array#flatten
|
168
168
|
#
|
169
169
|
def flatten(level = 1)
|
170
|
-
filter("flatten") do |
|
170
|
+
filter("flatten") do |yielder|
|
171
171
|
each do |element|
|
172
172
|
if level > 0 && element.respond_to?(:each)
|
173
|
-
element.flatten(level - 1).each(&
|
173
|
+
element.flatten(level - 1).each(&yielder)
|
174
174
|
else
|
175
|
-
|
175
|
+
yielder.call(element)
|
176
176
|
end
|
177
177
|
end
|
178
178
|
end
|
@@ -191,9 +191,9 @@ module Lazily
|
|
191
191
|
# @see ::Array#compact
|
192
192
|
#
|
193
193
|
def compact
|
194
|
-
filter("compact") do |
|
194
|
+
filter("compact") do |yielder|
|
195
195
|
each do |element|
|
196
|
-
|
196
|
+
yielder.call(element) unless element.nil?
|
197
197
|
end
|
198
198
|
end
|
199
199
|
end
|
@@ -239,11 +239,16 @@ module Lazily
|
|
239
239
|
end
|
240
240
|
|
241
241
|
def each
|
242
|
-
done = "done-#{object_id}".to_sym
|
243
242
|
return to_enum unless block_given?
|
244
243
|
yielder = proc { |x| yield x }
|
245
|
-
|
246
|
-
|
244
|
+
if @generator.arity == 2
|
245
|
+
# we might want to terminate early
|
246
|
+
all_done = "Lazily::loop-done-#{object_id}".to_sym
|
247
|
+
catch all_done do
|
248
|
+
@generator.call(yielder, all_done)
|
249
|
+
end
|
250
|
+
else
|
251
|
+
@generator.call(yielder)
|
247
252
|
end
|
248
253
|
end
|
249
254
|
|
data/lib/lazily/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazily
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-24 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! " Lazily implements \"lazy\" versions of many Enumerable methods,\n
|
15
15
|
\ allowing streamed processing of large (or even infinite) collections.\n\n It's
|
@@ -63,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
segments:
|
65
65
|
- 0
|
66
|
-
hash:
|
66
|
+
hash: 1657751678053576424
|
67
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
68
|
none: false
|
69
69
|
requirements:
|
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
version: '0'
|
73
73
|
segments:
|
74
74
|
- 0
|
75
|
-
hash:
|
75
|
+
hash: 1657751678053576424
|
76
76
|
requirements: []
|
77
77
|
rubyforge_project:
|
78
78
|
rubygems_version: 1.8.23
|