> Go's "problem" is that it can't efficiently give i a new address in each iteration: it'd have to put each one in a new memory location.
It's not an efficiency issue. How do you think Ruby does it?
It's not an efficiency issue. How do you think Ruby does it?
This applies to each-style loops. Actual Ruby for loops don't have this behaviour.
a = []
for i in 1..100
a << ->{ puts i } # append, to a, a function that prints 'i'
end
a[0].call()
This prints 100, since the scope of i covers the entire loop, and not each iteration. They are, however, much faster than each-style loops, since they don't have to make a stack frame for each iteration.[1]: https://blogs.msdn.microsoft.com/oldnewthing/20100809-00/?p=...