Thanks, this works nice when wrapping a for loop. But I've tried it (naively) 
in the follow way which fails

macro toIter
    first type mismatch at position: 1 required type for x: ForLoopStmt but 
expression 'fibonacci()' is of type: iterator (): int{.closure.}

Here is my example 
    
    
    import strutils, sequtils
    import macros
    {.experimental: "forLoopMacros".}
    
    macro toItr*(x : ForLoopStmt): untyped =
      let expr = x[0]
      let call = x[1][1]  # Get foo out of tolItr(foo)
      let body = x[2]
      result = quote do:
        block:
          let itr = `call`
          for `expr` in itr():
            `body`
    
    iterator firstn(g:iterator():int, n:int): int =
      for i in 0..<n :
        yield g()
    
    proc fibonacci(): iterator(): int =
      result = iterator(): int =
        var a, b : int
        a = 0; b= 1
        while true:
            yield a
            (a, b) = (b, a + b)
    
    for x in firstn(toItr(fibonacci()), 10) :
      echo x
    
    
    Run

Reply via email to