Class: Enumerator::Lazy
- Inherits:
-
Enumerator
- Object
- Enumerator
- Enumerator::Lazy
- Defined in:
- lib/lazy_enumerator.rb,
lib/lazy_enumerator/version.rb
Constant Summary collapse
- VERSION =
"2.0.0"
Instance Method Summary collapse
- #collect ⇒ Object (also: #map)
- #cycle(n = nil, &block) ⇒ Object
- #drop(n) ⇒ Object
- #drop_while(&block) ⇒ Object
- #flat_map(&block) ⇒ Object
- #grep(pattern, &block) ⇒ Object
-
#initialize ⇒ Lazy
constructor
A new instance of Lazy.
- #lazy ⇒ Object
- #reject(&block) ⇒ Object
- #select(&block) ⇒ Object
- #slice_before(pattern) ⇒ Object
- #take(n) ⇒ Object
- #take_while(&block) ⇒ Object
- #to_enum(name = nil, *args) ⇒ Object (also: #enum_for)
- #zip(*enumerables, &block) ⇒ Object
Constructor Details
#initialize ⇒ Lazy
Returns a new instance of Lazy.
5 6 7 8 |
# File 'lib/lazy_enumerator.rb', line 5 def initialize(*) _block_error(:new) unless block_given? super end |
Instance Method Details
#collect ⇒ Object Also known as: map
10 11 12 13 14 15 16 17 |
# File 'lib/lazy_enumerator.rb', line 10 def collect() _block_error(:collect) unless block_given? Lazy.new do |output| each do |element| output.yield yield(element) end end end |
#cycle(n = nil, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lazy_enumerator.rb', line 20 def cycle(n = nil, &block) if block if n n.times do each {|*elt| block.call(*elt)} end else loop do each {|*elt| block.call(*elt)} end end nil else if n Lazy.new do |output| n.times do each do |elt| output.yield elt end end end else Lazy.new do |output| loop do each do |elt| output.yield elt end end end end end end |
#drop(n) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/lazy_enumerator.rb', line 53 def drop(n) Lazy.new do |output| each_with_index do |element, index| next if index < n output.yield(element) end end end |
#drop_while(&block) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/lazy_enumerator.rb', line 62 def drop_while(&block) _block_error(:collect) unless block Lazy.new do |output| each do |element| output.yield(element) unless yield(element)..true end end end |
#flat_map(&block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lazy_enumerator.rb', line 73 def flat_map(&block) _block_error(:collect) unless block Lazy.new do |output| each do |element| result = yield element if result.is_a? Array ary = result elsif result.respond_to?(:force) && result.respond_to?(:each) lazy = result else ary = result.to_ary if result.respond_to?(:to_ary) end if ary # if it's an array or coerced to an array, iterate directly i, max = 0, ary.size while i < max output.yield ary.at(i) i+=1 end elsif lazy # if it's lazy, each over it lazy.each {|value| output.yield value} else # otherwise just yield it output.yield(result) end end end end |
#grep(pattern, &block) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazy_enumerator.rb', line 105 def grep(pattern, &block) if block_given? Lazy.new do |output| each do |element| output.yield(yield element) if pattern === element end end else Lazy.new do |output| each do |element| output.yield(element) if pattern === element end end end end |
#lazy ⇒ Object
120 121 122 |
# File 'lib/lazy_enumerator.rb', line 120 def lazy self end |
#reject(&block) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/lazy_enumerator.rb', line 124 def reject(&block) _block_error(:collect) unless block Lazy.new do |output| each do |element| output.yield(element) unless yield(element) end end end |
#select(&block) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/lazy_enumerator.rb', line 133 def select(&block) _block_error(:collect) unless block Lazy.new do |output| each do |element| output.yield(element) if yield(element) end end end |
#slice_before(pattern) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/lazy_enumerator.rb', line 142 def slice_before(pattern) Lazy.new do |output| slice = [] catch(output) do each do |element| if pattern === element slice << element else throw output end end end output.yield slice end end |
#take(n) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/lazy_enumerator.rb', line 158 def take(n) Lazy.new do |output| if n > 0 catch(output) do each_with_index do |element, index| output.yield(element) # break failed to work for some reason throw output if index + 1 == n end end end end end |
#take_while(&block) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/lazy_enumerator.rb', line 183 def take_while(&block) _block_error(:collect) unless block Lazy.new do |output| catch(output) do each do |element| # break failed to work for some reason throw output unless yield(element) output.yield(element) end end end end |
#to_enum(name = nil, *args) ⇒ Object Also known as: enum_for
172 173 174 175 176 177 178 179 180 |
# File 'lib/lazy_enumerator.rb', line 172 def to_enum(name = nil, *args) if name Lazy.new do |yielder| send(name, *args) {|element| yielder.yield(element)} end else self end end |
#zip(*enumerables, &block) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/lazy_enumerator.rb', line 196 def zip(*enumerables, &block) if block return super end all_arrays = true enumerators = enumerables.map do |enumerable| if enumerable.kind_of? Array next enumerable elsif enumerable.respond_to?(:to_ary) enumerator = enumerable.to_ary next enumerator unless enumerator.nil? end all_arrays = false next enumerable if enumerable.respond_to?(:each) raise TypeError, "wront argument type #{enumerable.class.name} (must respond to :each)" end if all_arrays Lazy.new do |output| each_with_index do |element, index| ary = [element] enumerators.each {|array| ary << array[index] if index < array.size} output.yield ary end end else Lazy.new do |output| enumerators = enumerators.map(&:to_enum) each do |element| ary = [element] enumerators.each {|enumerator| ary << (enumerator.next rescue nil)} output.yield ary end end end end |