Closure

Revised

What is Closure?

A closure is the combination of a function and the lexical environment within which that function was declared.

The reason it is called a “closure” is that an expression containing free variables is called an “open” expression, and by associating to it the bindings of its free variables, you close it

The concept of closure comes from Lambda Calculus (also written as λ-calculus). λ-calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation that can be used to simulate any Turing machine.

Yet, there is one basic difference between closure and λ-calculus. This can be understood when we consider a function as a black-box.

function h is some black-box in λ-calculus

In Closure, the function’s output is also influenced by other environment variables. Meaning, it can be influenced by variables (a.k.a environment variables, free variables, … as part of the same lexical scope) that are not formal parameters to the function.

Relationship(s)

At times other names are used inter-changeably with closure. This sections attempts to clarify on those.

Closure is one of the fundamental units of Functional Programing (FP). In fact, this is the basis for a higher order function in FP.

A higher order function is closure that returns another function. Example uses of this being the infamous map, reduce, filter.

A first class function is one where a language construct supports passing a closure as a parameter.

⛑ Suggestions / Feedback ! 😃