Module: Enumerable

Defined in:
lib/b-lazy.rb

Instance Method Summary collapse

Instance Method Details

#consObject

In Java-speak, we would say this method operates on Enumerable<Enumerable<?>> . It concatenates the values of each nested Enumerable and produces one Enumerable that contains all of the values. For example:

[1, 2, 3], [4, 5]].cons.to_a -> [1, 2, 3, 4, 5


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/b-lazy.rb', line 187

def cons()
  Enumerator.new do |out|
    s  = self.ensure_enum
    loop do
      items = s.next.ensure_enum
      loop do
        out.yield items.next
      end
    end
  end
end

#cycleObject

Keeps repeating the same elements indefinitely.



276
277
278
279
280
281
282
283
284
285
# File 'lib/b-lazy.rb', line 276

def cycle()
  Enumerator.new do |out|
    values = self.touch{|x| out.yield x}.to_a
    unless values.empty?
      loop do
        values.each{|x| out.yield x}
      end
    end
  end
end

#diagonalizeObject

If you have an infinite number of Enumerators, and each of these have an infinite number of elements, then you should iterate over them using Cantor’s diagonalization technique. As t -> infinity, you will examine all elements from all Enumerators.



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/b-lazy.rb', line 222

def diagonalize()
  Enumerator.new do |out|
    s = self.ensure_enum
    enums = []
    while s.has_next? do
      enums.unshift s.next.ensure_enum
      enums = enums.lselect{|e| e.has_next?}.touch{|e| out.yield e.next}.to_a
    end
    
    # Nothing else in s.  Just weave the remaining elements
    enums.weave.each{|x| out.yield x}
  end
end

#do_until(&blk) ⇒ Object Also known as: stop_before

Keep iterating until the condition becomes true



140
141
142
143
144
145
146
147
148
# File 'lib/b-lazy.rb', line 140

def do_until(&blk)
  s = self.ensure_enum
  Enumerator.new do |out|
    until s.empty?
      break if blk.call(s.peek)
      out.yield s.next
    end
  end
end

#do_while(&blk) ⇒ Object

Continue as long as the condition is true



128
129
130
131
132
133
134
135
136
# File 'lib/b-lazy.rb', line 128

def do_while(&blk)
  s = self.ensure_enum
  Enumerator.new do |out|
    while s.has_next?
      break unless blk.call(s.peek)
      out.yield s.next
    end
  end
end

#ensure_enumObject

When #to_enum is called on an Enumerator, it creates a copy and rewinds it (when possible). Unfortunately, this is not actually the behavior that we want; we just want to make sure that we’re operating on an Enumerator. So, this method calls #to_enum only if it is necessary.



312
313
314
315
316
317
318
# File 'lib/b-lazy.rb', line 312

def ensure_enum()
  if self.kind_of? Enumerator
    self
  else
    self.to_enum
  end
end

#lmap(&blk) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/b-lazy.rb', line 47

def lmap(&blk)
  Enumerator.new do |out|
    self.each do |i|
      out.yield blk.call(i)
    end
  end
end

#lreject(&blk) ⇒ Object

This is the same as the #reject method except that it operates lazily.



87
88
89
90
91
92
93
# File 'lib/b-lazy.rb', line 87

def lreject(&blk)
  Enumerator.new do |out|
    self.each do |i|
      out.yield i unless blk.call(i)
    end
  end
end

#lselect(&blk) ⇒ Object

This is the same as the #select method except that it operates lazily.



76
77
78
79
80
81
82
# File 'lib/b-lazy.rb', line 76

def lselect(&blk)
  Enumerator.new do |out|
    self.each do |i|
      out.yield i if blk.call(i)
    end
  end
end

#ltransposeObject

This is similar to Array’s transpose method, but it can operate on any Enumerable. Additionally, it stops as soon as the first Enumerable is exhausted (rather than setting the missing values equal to nil).



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/b-lazy.rb', line 258

def ltranspose()
  Enumerator.new do |out|
    catch(:nothing_to_do) do
      # If any Enumerable is empty, then yield nothing
      enums = self.lmap{|e| e.ensure_enum}.
                   touch{|e| throw :nothing_to_do if e.empty?}.
                   to_a
      
      loop do
        out.yield enums.map{|e| e.next}
      end
    end
  end
end

#randomly(n = 8) ⇒ Object

Randomizes the order in which the elements are yielded. Since this is done in a lazy fashion, it is not as random as actually shuffling the entire list.



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/b-lazy.rb', line 240

def randomly(n = 8)
  Enumerator.new do |out|
    s = self.ensure_enum
    pool = s.grab(n)
    while s.has_next?
      index = rand(n)
      out.yield pool[index]
      pool[index] = s.next
    end
    pool.sort_by{rand}.each{|x| out.yield x}
  end
end

#repeat(n) ⇒ Object

Repeats the same sequence of elements n times.



292
293
294
295
296
297
298
299
300
# File 'lib/b-lazy.rb', line 292

def repeat(n)
  n = n.to_i
  Enumerator.new do |out|
    if n >= 1
      values = self.touch{|x| out.yield x}.to_a
      (n - 1).times{ values.each{|x| out.yield x} }
    end
  end
end

#skip(n = 1) ⇒ Object

Skips the specified number of elements. Note that this method does not complain if the Enumerator is empty.



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/b-lazy.rb', line 167

def skip(n = 1)
  Enumerator.new do |out|
    s = self.ensure_enum
    begin
      n.times {s.next}
      loop do
        out.yield s.next
      end
    rescue StopIteration
    end
  end
end

#start_after(&blk) ⇒ Object

Start one element after the condition becomes true



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/b-lazy.rb', line 113

def start_after(&blk)
  Enumerator.new do |out|
    s = self.ensure_enum
    loop do
      break if blk.call(s.next)
    end
    
    loop do
      out.yield s.next
    end
  end
end

#start_when(&blk) ⇒ Object

Begins yielding values as soon as the condition becomes true.



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/b-lazy.rb', line 97

def start_when(&blk)
  Enumerator.new do |out|
    s = self.ensure_enum
    loop do
      break if blk.call(s.peek)
      s.next
    end
    
    loop do
      out.yield s.next
    end
  end
end

#stop_when(&blk) ⇒ Object

Returns all elements up to and including the element the causes the condition to become true.



154
155
156
157
158
159
160
161
162
# File 'lib/b-lazy.rb', line 154

def stop_when(&blk)
  s = self.ensure_enum
  Enumerator.new do |out|
    while s.has_next?
      out.yield s.peek
      break if blk.call(s.next)
    end
  end
end

#touch(&blk) ⇒ Object

This is similar to the tap method, but it operates on each element of the Enumerator as it passes through. This is useful for many things, such as:

  1. Examining the state of the element

  2. Logging

  3. Modifying the element’s state

  4. Recording interim values



64
65
66
67
68
69
70
71
# File 'lib/b-lazy.rb', line 64

def touch(&blk)
  Enumerator.new do |out|
    self.each do |x|
      blk.call x
      out.yield x
    end
  end
end

#weaveObject

This is similar to cons, but it instead takes the first item from each enumerator, then the second item, etc. This can be handy when you have a finite number of enumerators, but each one may hold an infinite number of items



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/b-lazy.rb', line 205

def weave()
  Enumerator.new do |out|
    enums = self.ensure_enum.lmap(&:ensure_enum)
    
    while enums.has_next? do
      # We to_a each iteration to avoid creating a huge
      # Enumerator stack.
      enums = enums.lselect{|e| e.has_next?}.touch{|e| out.yield e.next}.to_a.ensure_enum
    end
  end
end