- Define
a closure is a data structure storing a function together with an environment: a mapping associating each free variable of the function with the value or storage location the name was bound to at the time the closure was created.
A closure—unlike a plain function—allows the function to access those captured variables through the closure’s reference to them, even when the function is invoked outside their scope.
The use of closures is associated with languages where functions are first-class objects.
- Applications
- Function factories, create functions with args.
- Emulating private methods with closures
- Some examples in python, scheme and golang:
|
|
|
|
|
|