If I use Ruby Enumerators to implement a generator and a filter:
generator = Enumerator.new do |y|
x = 0
loop do
y << x
x += 1
break if x > CUTOFF
end
end.lazy
filter = Enumerator.new do |y|
loop do
i = generator.next
y << i if i.even?
end
end
does it make any difference whether I break out of the generator's loop using
break if x > CUTOFF
vs
raise StopIteration if x > CUTOFF
?
Both seem to work. I imagine break
would be more performant, but is raise
more idiomatic here?
In Ruby it's considered a bad practice to use raise/fail
for control flow as they are really slow.
So to answer your question raise StopIteration if x > CUTOFF
isn't an idiomatic way of breaking from the loop