Breaking An Iteration, Then Running It Again, Unexpected Result?

Using Renoise 2.7.2 with a pattern longer than 10 lines, consider the following code:

  
local rns = renoise.song()  
local my_pattern = rns.selected_pattern_index  
local my_track = rns.selected_track_index  
local my_iter = rns.pattern_iterator:lines_in_pattern_track(my_pattern, my_track)  
  
for pos,line in my_iter do  
 print(pos.line)  
 if pos.line >= 10 then break end  
end  
  
print("--- Start again?! ---")  
  
for pos,line in my_iter do  
 print(pos.line)  
end  
  

Expected: After “Start Again”, iteration is reset to 1.
Actual: Continues at 11?

The “break” statement in the loop does nothing to the iterator itself, so this is expected behavior. You maybe expect this, because you usually do not cache iterators outside of the loop, but do something like this instead:

  
local rns = renoise.song()  
local my_pattern = rns.selected_pattern_index  
local my_track = rns.selected_track_index  
local iter = rns.pattern_iterator  
  
for pos,line in iter:lines_in_pattern_track(my_pattern, my_track) do  
 print(pos.line)  
 if pos.line >= 10 then break end  
end  
  
-- restarts  
for pos,line in iter:lines_in_pattern_track(my_pattern, my_track) do  
 print(pos.line)  
end  
  

Actually it’s pretty usefull to be able to continue the iteration, if you really want to do so:

  
local rns = renoise.song()  
local my_pattern = rns.selected_pattern_index  
local my_track = rns.selected_track_index  
local my_iter = rns.pattern_iterator:lines_in_pattern_track(my_pattern, my_track)  
  
for pos,line in my_iter do  
 print(pos.line)  
 if pos.line >= 10 then break end  
end  
  
-- continue at 11  
for pos,line in my_iter do  
 print(pos.line)  
end  
  
-- restart  
my_iter = rns.pattern_iterator:lines_in_pattern_track(my_pattern, my_track)  
for pos,line in my_iter do  
 print(pos.line)  
end  
  
  

Ok, so expected behaviour then. I can live with that.

Cheers.