Skip to content

Commit aeac2ee

Browse files
author
jethro larson
committed
Simplified closure definition hemanth#162
1 parent b10eba9 commit aeac2ee

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

readme.md

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ __Table of Contents__
2121

2222
* [Arity](#arity)
2323
* [Higher-Order Functions (HOF)](#higher-order-functions-hof)
24+
* [Closure](#closure)
2425
* [Partial Application](#partial-application)
2526
* [Currying](#currying)
26-
* [Closure](#closure)
2727
* [Auto Currying](#auto-currying)
2828
* [Function Composition](#function-composition)
2929
* [Continuation](#continuation)
@@ -94,6 +94,43 @@ const is = (type) => (x) => Object(x) instanceof type
9494
filter(is(Number), [0, '1', 2, null]) // [0, 2]
9595
```
9696

97+
## Closure
98+
99+
A closure is a scope which retains variables available to a function when it's created. This is important for
100+
[partial application](#partial-application) to work.
101+
102+
103+
```js
104+
const addTo = (x) => {
105+
return (y) => {
106+
return x + y
107+
}
108+
}
109+
```
110+
111+
We can call `addTo` with a number and get back a function with a baked-in `x`.
112+
113+
```js
114+
var addToFive = addTo(5)
115+
```
116+
117+
In this case the `x` is retained in `addToFive`'s closure with the value `5`. We can then call `addToFive` with the `y`
118+
and get back the desired number.
119+
120+
```
121+
addToFive(3) // => 8
122+
```
123+
124+
This works because variables that are in parent scopes are not garbage-collected as long as the function itself is retained.
125+
126+
Closures are commonly used in event handlers so that they still have access to variables defined in their parents when they
127+
are eventually called.
128+
129+
__Further reading__
130+
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
131+
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
132+
133+
97134
## Partial Application
98135

99136
Partially applying a function means creating a new function by pre-filling some of the arguments to the original function.
@@ -125,6 +162,7 @@ const add1More = add3.bind(null, 2, 3) // (c) => 2 + 3 + c
125162

126163
Partial application helps create simpler functions from more complex ones by baking in data when you have it. [Curried](#currying) functions are automatically partially applied.
127164

165+
128166
## Currying
129167

130168
The process of converting a function that takes multiple arguments into a function that takes them one at a time.
@@ -144,38 +182,6 @@ add2(10) // 12
144182

145183
```
146184

147-
## Closure
148-
149-
A closure is a way of accessing a variable outside its scope.
150-
Formally, a closure is a technique for implementing lexically scoped named binding. It is a way of storing a function with an environment.
151-
152-
A closure is a scope which captures local variables of a function for access even after the execution has moved out of the block in which it is defined.
153-
ie. they allow referencing a scope after the block in which the variables were declared has finished executing.
154-
155-
156-
```js
157-
const addTo = x => y => x + y
158-
var addToFive = addTo(5)
159-
addToFive(3) // returns 8
160-
```
161-
162-
The function `addTo()` returns a function(internally called `add()`), lets store it in a variable called `addToFive` with a curried call having parameter 5.
163-
164-
Ideally, when the function `addTo` finishes execution, its scope, with local variables add, x, y should not be accessible. But, it returns 8 on calling `addToFive()`. This means that the state of the function `addTo` is saved even after the block of code has finished executing, otherwise there is no way of knowing that `addTo` was called as `addTo(5)` and the value of x was set to 5.
165-
166-
Lexical scoping is the reason why it is able to find the values of x and add - the private variables of the parent which has finished executing. This value is called a Closure.
167-
168-
The stack along with the lexical scope of the function is stored in form of reference to the parent. This prevents the closure and the underlying variables from being garbage collected(since there is at least one live reference to it).
169-
170-
Lambda Vs Closure: A lambda is essentially a function that is defined inline rather than the standard method of declaring functions. Lambdas can frequently be passed around as objects.
171-
172-
A closure is a function that encloses its surrounding state by referencing fields external to its body. The enclosed state remains across invocations of the closure.
173-
174-
175-
__Further reading/Sources__
176-
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
177-
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
178-
179185
## Auto Currying
180186
Transforming a function that takes multiple arguments into one that if given less than its correct number of arguments returns a function that takes the rest. When the function gets the correct number of arguments it is then evaluated.
181187

0 commit comments

Comments
 (0)