As far as I could understand this Nim code 
    
    
    var arr = newSeq[proc()]()
    for j in 0..<10:
        let x : int = j + 1
        let p = proc() = echo x
        arr.add(p)
    
    for p in arr:
        p()
    
    

should be equivalent to the following Javascript code 
    
    
    var arr = []
    for (let j=0; j<10; j++)
    {
        let x = j + 1
        let p = () => console.log(x)
        arr.push(p)
    }
    for(p of arr) p()
    

actually the Nim version always prints 10 instead of the whole sequence from 1 
to 10.. I cannot understand how lambda capture works since the captured 
variable x should local to scope of the for loop and a new instance should be 
created for every iteration. Also, is there a way to capture variables by 
value? How could I fix the previous example to make it functionally equivalent 
to the Javascript one?

Reply via email to