Closure iterators not using a generator require passing in the state, when you 
pass in `1, 2` it finishes the iterator since it already iterated to `2`.

The 'proper' way of doing what you want is to write a closure generator that 
stores the state on instantiation:
    
    
    proc makeMyClos(a, b: int): iterator: int =
      result = iterator: int {.closure.} =
        for x in a..b:
          yield x
    
    proc f(it: iterator) =
      echo it()
      echo it()
      echo it()
      echo it()
    
    f(makeMyClos(1, 4))
    
    
    Run

This stores the creation state with the iterator so one does not need to pass 
the data and is a more understandable construct. Not that there is no use case 
for a normal iterator. (My Package Slicerator contains a 
[asClosure](https://github.com/beef331/slicerator/blob/master/src/closures.nim#L60-L62)
 macro that handles this automatically

Reply via email to