I asked a question about Currying and closures were mentioned. What is a closure? How does it relate to currying?
相关问题
- Relation between Function1 and Reader Monad
- scala passing function with underscore produces a
- Combining n vectors into one vector of n-tuples
- Improve this code by eliminating nested for cycles
- Redefine list monad instance
相关文章
- Is there something like the threading macro from C
- Learning F#: What books using other programming la
- Creating a list of functions using a loop in R
- What does “exposition only” mean? Why use it?
- When to use interfaces, and when to use higher ord
- Functors in Ocaml
- Java Lambda Referencing Enclosing Object: Replace
- “Adapter” or “adaptor”?
From Lua.org:
In short, function pointer is just a pointer to a location in the program code base (like program counter). Whereas Closure = Function pointer + Stack frame.
.
If you are from the Java world, you can compare a closure with a member function of a class. Look at this example
The function
g
is a closure:g
closesa
in. Sog
can be compared with a member function,a
can be compared with a class field, and the functionf
with a class.Please have a look below code to understand closure in more deep:
Here what will be output?
0,1,2,3,4
not that will be5,5,5,5,5
because of closureSo how it will solve? Answer is below:
Let me simple explain, when a function created nothing happen until it called so for loop in 1st code called 5 times but not called immediately so when it called i.e after 1 second and also this is asynchronous so before this for loop finished and store value 5 in var i and finally execute
setTimeout
function five time and print5,5,5,5,5
Here how it solve using IIFE i.e Immediate Invoking Function Expression
For more, please understand execution context to understand closure.
There is one more solution to solve this using let (ES6 feature) but under the hood above function is worked
=> More explanation:
In memory, when for loop execute picture make like below:
Loop 1)
Loop 2)
Loop 3)
Loop 4)
Loop 5)
Here i is not executed and then after complete loop, var i stored value 5 in memory but it's scope is always visible in it's children function so when function execute inside
setTimeout
out five time it prints5,5,5,5,5
so to resolve this use IIFE as explain above.
Kyle's answer is pretty good. I think the only additional clarification is that the closure is basically a snapshot of the stack at the point that the lambda function is created. Then when the function is re-executed the stack is restored to that state before executing the function. Thus as Kyle mentions, that hidden value (
count
) is available when the lambda function executes.Here is another real life example, and using a scripting language popular in games - Lua. I needed to slightly change the way a library function worked to avoid a problem with stdin not being available.
The value of old_dofile disappears when this block of code finishes it's scope (because it's local), however the value has been enclosed in a closure, so the new redefined dofile function CAN access it, or rather a copy stored along with the function as an 'upvalue'.