Module: Enumerable
- Defined in:
- lib/lazer/lazy_enumerable.rb
Instance Method Summary collapse
- #at_lazy(n) ⇒ Object
- #drop_lazy(n) ⇒ Object
- #drop_while_lazy(&block) ⇒ Object
- #flatten_lazy(&block) ⇒ Object
- #grep_lazy(pattern, &block) ⇒ Object
- #map_lazy(&block) ⇒ Object (also: #collect_lazy)
- #reject_lazy(&block) ⇒ Object
- #select_lazy(&block) ⇒ Object (also: #find_all_lazy)
- #slice_lazy(index, length = 1) ⇒ Object
- #take_lazy(n) ⇒ Object
- #take_while_lazy(&block) ⇒ Object
-
#zip_lazy(*args, &block) ⇒ Object
this is disgusting, and the reason why Enumerator needs a has_next?.
Instance Method Details
#at_lazy(n) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/lazer/lazy_enumerable.rb', line 2 def at_lazy(n) self.each_with_index do |val, index| return val if index == n end nil end |
#drop_lazy(n) ⇒ Object
9 10 11 |
# File 'lib/lazer/lazy_enumerable.rb', line 9 def drop_lazy(n) get_some{|yielder, val, index| yielder << val if index >= n} end |
#drop_while_lazy(&block) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/lazer/lazy_enumerable.rb', line 13 def drop_while_lazy(&block) found = false get_some do |yielder, val| next if block.(val) && !found found = true yielder << val end end |
#flatten_lazy(&block) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/lazer/lazy_enumerable.rb', line 26 def flatten_lazy(&block) get_some do |yielder, val| block.(val).each do |inner_val| yielder << inner_val end end end |
#grep_lazy(pattern, &block) ⇒ Object
22 23 24 |
# File 'lib/lazer/lazy_enumerable.rb', line 22 def grep_lazy(pattern, &block) get_some {|yielder, val| yielder << (block_given? ? block.(val) : val) if pattern === val} end |
#map_lazy(&block) ⇒ Object Also known as: collect_lazy
34 35 36 |
# File 'lib/lazer/lazy_enumerable.rb', line 34 def map_lazy(&block) get_some {|yielder, val| yielder << block.(val)} end |
#reject_lazy(&block) ⇒ Object
40 41 42 |
# File 'lib/lazer/lazy_enumerable.rb', line 40 def reject_lazy(&block) self.select_lazy{|val| !block.(val)} end |
#select_lazy(&block) ⇒ Object Also known as: find_all_lazy
44 45 46 |
# File 'lib/lazer/lazy_enumerable.rb', line 44 def select_lazy(&block) get_some {|yielder, val| yielder << val if block.(val)} end |
#slice_lazy(index, length = 1) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lazer/lazy_enumerable.rb', line 50 def slice_lazy(index, length=1) start, finish = index.begin, index.end if index.instance_of? Range start, finish = index, index + length - 1 if index.instance_of? Fixnum get_some do |yielder, val, index| next if index < start throw :done if index > finish yielder << val end end |
#take_lazy(n) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/lazer/lazy_enumerable.rb', line 61 def take_lazy(n) get_some do |yielder, val, index| yielder << val throw :done if index == n-1 end end |
#take_while_lazy(&block) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/lazer/lazy_enumerable.rb', line 68 def take_while_lazy(&block) get_some do |yielder, val| throw :done unless block.(val) yielder << val end end |
#zip_lazy(*args, &block) ⇒ Object
this is disgusting, and the reason why Enumerator needs a has_next?
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lazer/lazy_enumerable.rb', line 86 def zip_lazy(*args, &block) rators = ([self] + args).map{|arr| arr.each} Enumerator.new do |yielder| loop do answer = rators.map do |rator| begin [rator.better_next, true] rescue StopIteration [nil, false] end end break if answer.none?{|val, signal| signal} output = answer.map{|val, signal| val} output = block.(output) if block_given? yielder << output end end end |