Scala Closures || Anonymous Functions || Lambda Expressions || Functional Programming

The title gives four keywords because they are very relevant

I have never understood 闭包this word before, and I have been fascinated and confused.

What callback function is an example of a closure and so on...

Closures are functions that can read variables inside other functions. For example, in javascript, only sub-functions inside functions can read local variables, so closures can be understood as "functions defined inside a function". In essence, closures are the bridge between the inside of the function and the outside of the function.
--Baidu Encyclopedia

The closest concept to closures that Java provides today is lambda expressions, and while there are significant differences between closures and lambda expressions, at least lambda expressions are a good replacement for closures.

To understand closures, it is best to look at a more pure functional programming language, such as js or scala

write picture description here

A layer of functions is set on the input parameters, and the original parameters are processed and returned to the final parameters. The function inside this function body is the closure, which is closed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325846765&siteId=291194637