I've been looking at Scala recently, and I've talked about closures and some of its benefits, but it's just that you don't see what counts as closures, please explain them in plain language.
Reply content:
Known as a function f (x) = x + I, let you ask f (3) = 3+i.
Analysis: To get the final function value, you must know the value of I. I is called the Open item ("Open", corresponding closure of "closed"), if the above definition of "Inti = 1", you can get f (3) = 3+1 = 4, that is, the function value if you want to be created must capture the value of I, this process can be understood to do the function "close" operation, so called closure. Closures
"An object was data with functions. A closure is a function with data. "-john D Cook
I was watching functional programming. Advanced R.
Time to see. It feels good to be an explanation. Xie invited
There used to be a PowerPoint share of functions and closures: sharing functions and closures in Ppt:scala
And the example in this blog is the typical binding problem in closures: closure variable binding
I hope it works for you. Closures in the programming language are "function objects." This means that it can be called as a function, or it can be assigned as an object.
Some languages such as Py and JS, each function is a closed packet. But in other languages, such as C #, the normal function is not a closure, only the delegate is a closed packet. Closures are the context in which the compiler creates a piece of code to execute.
Well, that's it, just keep looking. In a sense, all functions are closures, the functions in C/s + + are special closures, no free variables, no states, save these states or free variables, Python is Nonlocal,lua upvalue. You can use class implementations to mimic stateful closures before c++11. Closures occur because lexical scope, closures are composed of functions and environments, Scala should support functions as parameters or return values, then if there is no closure, then the function of the free variable will be wrong book learning this thing belongs to the vain. Let's take a look around and start writing code. Write it off so you can understand Scala's beauty closure is a function with a state, effective C + + 3rd on the implementation of a singleton pattern, can be considered a closure. A simple explanation of what the individual thinks, closures are a
Data, which not only covers the operations that can be performed on the data (that is, functions), but also the data that the function wants to manipulate (that is, the environment at that time).