Class: Enumerator

Inherits:
Object show all
Defined in:
lib/lazy_enumerator.rb

Instance Method Summary collapse

Instance Method Details

#lazy_map(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/lazy_enumerator.rb', line 23

def lazy_map(&block)
  Enumerator.new do |yielder| 
    self.each do |value| 
      yielder.yield(block.call(value))
    end
  end
end

#lazy_select(&block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/lazy_enumerator.rb', line 15

def lazy_select(&block)
  Enumerator.new do |yielder| 
    self.each do |val| 
      yielder.yield(val) if block.call(val) 
    end
  end
end

#lazy_sort_by(&block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/lazy_enumerator.rb', line 3

def lazy_sort_by(&block)
  Enumerator.new do |yielder|
    index = 0
    sorted = sort_by(&block)
    loop do
      yielder.yield sorted[index]
      index += 1
      break if sorted.size == index
    end
  end
end