For some reason, I tend to associate closures with functional languages. I believe this is mainly due to the fact that the discussions that I saw regarding closure are almost always in an environment that focuses on functional programming. At the same time, the actual practical use of closures, which I can think of, is all non-functional.
Are there practical applications of closures in functional languages or an association in my mind, mainly because closures are used for programming in a style that is also common to functional programming languages (first-class functions, currying, etc.)?
Edit: I have to clarify that I refer to the actual functional languages, that is, I was looking for applications that preserve referential transparency (for the same input, you get the same result).
Edit: Add a summary of what has been published so far:
- Closure is used to perform a partial assessment. In particular, for a function that takes two arguments, it can be called with one argument, which returns a function that takes one argument. Typically, the method by which this second function “stores” the first value passed to it is a closure.
- Objects can be implemented using closures. A function is returned that closes next to a series of variables and can then use them as attributes of objects. The function itself can return more methods that act as object methods that also have access to these variables. Assuming the variables are not changed, referential transparency is maintained.
source
share