-
-
Save remram44/5833870 to your computer and use it in GitHub Desktop.
| #include <functional> | |
| #include <iostream> | |
| #include <vector> | |
| int main() | |
| { | |
| std::vector<std::function<int (int)>> l; | |
| for(int i = 0; i < 10; ++i) | |
| l.push_back([=](int x) { return i + x; }); | |
| std::cout << l[2](3) << std::endl; // 5 | |
| return 0; | |
| } |
| l = {} | |
| for i = 0, 10 do | |
| l[i] = function(x) return x + i end | |
| end | |
| print(l[2](3)) | |
| -- 5 |
| l = [] | |
| for i in range(10): | |
| l.append(lambda x: x + i) | |
| print(l[2](3)) | |
| # 12 |
Indeed, perhaps I got confused by comment above stating:
you can mutate the upvalue in Lua (and C++ if you add the 'mutable' keyword), not in Python:
Indeed, maybe it says only about the specific upvalue in the specific code snipper, not about an upvalue at all. (You may quite guess that my native language doesn't have definite/indefinite articles.)
Anyway, thanks for both posting the gist and being around to discuss it. My coming here is based merely on a google search regarding whether people use term "upvalue" in regard to Python (the official docs don't use it). And I just couldn't resist commenting on a seemingly incorrect statement, for the sake of beginners who may come here ;-).
I don't think Lua upvalues actually work differently to Python's in the respect you demonstrated, but their for loops do. E.g.:
l = {}
local i = 0
while true do
l[i] = function(x) return x + i end
if i + 1 > 9 then break end
i = i + 1
end
print(l[2](3))
-- 12Each i is a different local in the for loop.
That's one way to look at it, sure
Anyway, I also found this while curious about whether Python uses the term upvalue :P
The Python documentation refers to them as "cells", see for example in data model and PyFunction.
Cool, thank you!
Congratulations, you get the point of this Gist, which is to show the different behaviors between those languages. I did not claim
nonlocaldid not work as expected or did anything to help in this situation, you did.