In this example the fiber is terminated after one yield. I expected the data_rows.each enumerator to provide repeated fiber.yields until the data or events are exhausted. I want to make a single pass through the data while processing all events rather than walking the data file for each event. I have code that does this the slow way. But since I will be running this on thousands of data sets use of Fiber seems appropriate.
This example is simplified from a real world need to repeatedly process tens of millions of data samples for analysis of pre-identified events. The data is sorted by sample # but there are gaps in the sample #s. Thus sample # is NOT the position in the array. Events are sparsely located within the data and do not overlap.
class Example
def run
fiber = Fiber.new do
data_rows.each do |row|
next unless @onset == row[0]
p row
Fiber.yield if row[0] == @completion
end
end
. events.each do |evnt|
@onset, @completion = *evnt
fiber.resume
end
end
# [start sample #, completion sample #]
def events
[ [2,3], [9,20] ]
end
# Test data is sorted by sample # but there are gaps in the sample #s
# [sample #, datum]
def data_rows
[ [0, 'A'], [2, 'B'], [3, 'C'], [7, 'D'], [9, 'F'], [20, 'G'], [30, 'H'] ]
end
end
Example.new.run```