lazy_enumerator 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lazy_enumerator.gemspec +23 -0
- data/lib/lazy_enumerator.rb +275 -0
- data/lib/lazy_enumerator/version.rb +5 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Charles Oliver Nutter
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# lazy_enumerator
|
2
|
+
|
3
|
+
A pure-ruby implementation of Ruby 2.0's Enumerator::Lazy.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'lazy_enumerator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install lazy_enumerator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
See http://ruby-doc.org/core-2.0/Enumerator/Lazy.html
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lazy_enumerator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lazy_enumerator"
|
8
|
+
spec.version = Enumerator::Lazy::VERSION
|
9
|
+
spec.authors = ["Charles Oliver Nutter"]
|
10
|
+
spec.email = ["headius@headius.com"]
|
11
|
+
spec.description = %q{A pure-Ruby implementation of Ruby 2.0's Enumerator::Lazy}
|
12
|
+
spec.summary = %q{An implementation of Enumerator::Lazy for Ruby 1.9.3 (and probably 1.8.7).}
|
13
|
+
spec.homepage = "https://github.com/headius/lazy_enumerator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,275 @@
|
|
1
|
+
class Enumerator
|
2
|
+
|
3
|
+
class Lazy < Enumerator
|
4
|
+
|
5
|
+
def initialize(*)
|
6
|
+
_block_error(:new) unless block_given?
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def collect()
|
11
|
+
_block_error(:collect) unless block_given?
|
12
|
+
Lazy.new do |output|
|
13
|
+
each do |element|
|
14
|
+
output.yield yield(element)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
alias map collect
|
19
|
+
|
20
|
+
def cycle(n = nil, &block)
|
21
|
+
if block
|
22
|
+
if n
|
23
|
+
n.times do
|
24
|
+
each {|*elt| block.call(*elt)}
|
25
|
+
end
|
26
|
+
else
|
27
|
+
loop do
|
28
|
+
each {|*elt| block.call(*elt)}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
nil
|
32
|
+
else
|
33
|
+
if n
|
34
|
+
Lazy.new do |output|
|
35
|
+
n.times do
|
36
|
+
each do |elt|
|
37
|
+
output.yield elt
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
Lazy.new do |output|
|
43
|
+
loop do
|
44
|
+
each do |elt|
|
45
|
+
output.yield elt
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def drop(n)
|
54
|
+
Lazy.new do |output|
|
55
|
+
each_with_index do |element, index|
|
56
|
+
next if index < n
|
57
|
+
output.yield(element)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def drop_while(&block)
|
63
|
+
_block_error(:collect) unless block
|
64
|
+
Lazy.new do |output|
|
65
|
+
each do |element|
|
66
|
+
output.yield(element) unless yield(element)..true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
alias force to_a
|
72
|
+
|
73
|
+
def flat_map(&block)
|
74
|
+
_block_error(:collect) unless block
|
75
|
+
Lazy.new do |output|
|
76
|
+
each do |element|
|
77
|
+
result = yield element
|
78
|
+
if result.is_a? Array
|
79
|
+
ary = result
|
80
|
+
elsif result.respond_to?(:force) && result.respond_to?(:each)
|
81
|
+
lazy = result
|
82
|
+
else
|
83
|
+
ary = result.to_ary if result.respond_to?(:to_ary)
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
if ary
|
88
|
+
# if it's an array or coerced to an array, iterate directly
|
89
|
+
i, max = 0, ary.size
|
90
|
+
while i < max
|
91
|
+
output.yield ary.at(i)
|
92
|
+
i+=1
|
93
|
+
end
|
94
|
+
elsif lazy
|
95
|
+
# if it's lazy, each over it
|
96
|
+
lazy.each {|value| output.yield value}
|
97
|
+
else
|
98
|
+
# otherwise just yield it
|
99
|
+
output.yield(result)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def grep(pattern, &block)
|
106
|
+
if block_given?
|
107
|
+
Lazy.new do |output|
|
108
|
+
each do |element|
|
109
|
+
output.yield(yield element) if pattern === element
|
110
|
+
end
|
111
|
+
end
|
112
|
+
else
|
113
|
+
Lazy.new do |output|
|
114
|
+
each do |element|
|
115
|
+
output.yield(element) if pattern === element
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
def lazy
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
def reject(&block)
|
125
|
+
_block_error(:collect) unless block
|
126
|
+
Lazy.new do |output|
|
127
|
+
each do |element|
|
128
|
+
output.yield(element) unless yield(element)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def select(&block)
|
134
|
+
_block_error(:collect) unless block
|
135
|
+
Lazy.new do |output|
|
136
|
+
each do |element|
|
137
|
+
output.yield(element) if yield(element)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def slice_before(pattern)
|
143
|
+
Lazy.new do |output|
|
144
|
+
slice = []
|
145
|
+
catch(output) do
|
146
|
+
each do |element|
|
147
|
+
if pattern === element
|
148
|
+
slice << element
|
149
|
+
else
|
150
|
+
throw output
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
output.yield slice
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def take(n)
|
159
|
+
Lazy.new do |output|
|
160
|
+
if n > 0
|
161
|
+
catch(output) do
|
162
|
+
each_with_index do |element, index|
|
163
|
+
output.yield(element)
|
164
|
+
# break failed to work for some reason
|
165
|
+
throw output if index + 1 == n
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def to_enum(name = nil, *args)
|
173
|
+
if name
|
174
|
+
Lazy.new do |yielder|
|
175
|
+
send(name, *args) {|element| yielder.yield(element)}
|
176
|
+
end
|
177
|
+
else
|
178
|
+
self
|
179
|
+
end
|
180
|
+
end
|
181
|
+
alias enum_for to_enum
|
182
|
+
|
183
|
+
def take_while(&block)
|
184
|
+
_block_error(:collect) unless block
|
185
|
+
Lazy.new do |output|
|
186
|
+
catch(output) do
|
187
|
+
each do |element|
|
188
|
+
# break failed to work for some reason
|
189
|
+
throw output unless yield(element)
|
190
|
+
output.yield(element)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def zip(*enumerables, &block)
|
197
|
+
if block
|
198
|
+
return super
|
199
|
+
end
|
200
|
+
|
201
|
+
all_arrays = true
|
202
|
+
enumerators = enumerables.map do |enumerable|
|
203
|
+
if enumerable.kind_of? Array
|
204
|
+
next enumerable
|
205
|
+
elsif enumerable.respond_to?(:to_ary)
|
206
|
+
enumerator = enumerable.to_ary
|
207
|
+
|
208
|
+
next enumerator unless enumerator.nil?
|
209
|
+
end
|
210
|
+
|
211
|
+
all_arrays = false
|
212
|
+
next enumerable if enumerable.respond_to?(:each)
|
213
|
+
|
214
|
+
raise TypeError, "wront argument type #{enumerable.class.name} (must respond to :each)"
|
215
|
+
end
|
216
|
+
|
217
|
+
if all_arrays
|
218
|
+
Lazy.new do |output|
|
219
|
+
each_with_index do |element, index|
|
220
|
+
ary = [element]
|
221
|
+
enumerators.each {|array| ary << array[index] if index < array.size}
|
222
|
+
output.yield ary
|
223
|
+
end
|
224
|
+
end
|
225
|
+
else
|
226
|
+
Lazy.new do |output|
|
227
|
+
enumerators = enumerators.map(&:to_enum)
|
228
|
+
each do |element|
|
229
|
+
ary = [element]
|
230
|
+
enumerators.each {|enumerator| ary << (enumerator.next rescue nil)}
|
231
|
+
output.yield ary
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def _block_error(name)
|
238
|
+
raise ArgumentError.new("tried to call lazy #{name} without a block")
|
239
|
+
end
|
240
|
+
private :_block_error
|
241
|
+
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
module Enumerable
|
246
|
+
def lazy
|
247
|
+
Enumerator::Lazy.new(self) do |output, *values|
|
248
|
+
output.yield(*values)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def chunk(init_val = (init_given = true; nil))
|
253
|
+
cls = (self.kind_of? Enumerator) ? self.class : Enumerator
|
254
|
+
cls.new do |yielder|
|
255
|
+
|
256
|
+
chunk = nil
|
257
|
+
each do |elt|
|
258
|
+
new_val = init_given ? yield(elt, init_val) : yield(elt)
|
259
|
+
|
260
|
+
if chunk.nil?
|
261
|
+
chunk = [new_val, [elt]]
|
262
|
+
elsif new_val == init_val
|
263
|
+
chunk[1] << elt
|
264
|
+
else
|
265
|
+
yielder.yield chunk
|
266
|
+
chunk = [new_val, [elt]]
|
267
|
+
end
|
268
|
+
|
269
|
+
init_val = new_val
|
270
|
+
end
|
271
|
+
|
272
|
+
yielder.yield chunk if chunk
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_enumerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 2.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Charles Oliver Nutter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :development
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: !binary |-
|
37
|
+
MA==
|
38
|
+
none: false
|
39
|
+
requirement: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: !binary |-
|
44
|
+
MA==
|
45
|
+
none: false
|
46
|
+
prerelease: false
|
47
|
+
type: :development
|
48
|
+
description: A pure-Ruby implementation of Ruby 2.0's Enumerator::Lazy
|
49
|
+
email:
|
50
|
+
- headius@headius.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- ".gitignore"
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lazy_enumerator.gemspec
|
61
|
+
- lib/lazy_enumerator.rb
|
62
|
+
- lib/lazy_enumerator/version.rb
|
63
|
+
homepage: https://github.com/headius/lazy_enumerator
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: !binary |-
|
75
|
+
MA==
|
76
|
+
none: false
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: !binary |-
|
82
|
+
MA==
|
83
|
+
none: false
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.24
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: An implementation of Enumerator::Lazy for Ruby 1.9.3 (and probably 1.8.7).
|
90
|
+
test_files: []
|