During reading mastering Nim I found that I do not fully understand the closure 
iterators

I think that the main magic for me was mentioned in "resumable function" I 
cannot undertand

I created some playground: <https://wandbox.org/permlink/zDz2jv0Z9aaJo4Jy>
    
    
    iterator ab(a, b: int): int {.closure.} =
      for x in a..b:
        yield x
    
    proc f(it: iterator) =
      echo it(1, 4)
      echo it(1, 4)
      echo it(1, 2)
      echo it(1, 4)
    
    f(ab)
    
    
    Run

Result is
    
    
    1
    2
    0
    0
    
    
    Run

I see than initialization happens during first call of `it`, after it iterator 
falls into `for`. But my brain absolutely blowing that the 3rd pass of the `(1, 
2)` brakes for somehow iterator entered already and forces it finish

How is it possible? 

Reply via email to