Skip to content

Commit 0b36d32

Browse files
authored
remove duplicated closure section
1 parent f62ce45 commit 0b36d32

File tree

1 file changed

+16
-54
lines changed

1 file changed

+16
-54
lines changed

readme.md

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -102,40 +102,33 @@ filter(is(Number), [0, '1', 2, null]) // [0, 2]
102102

103103
## Closure
104104

105-
A closure is a scope which retains variables available to a function when it's created. This is important for
106-
[partial application](#partial-application) to work.
107-
105+
A closure is a way of accessing a variable outside its scope.
106+
Formally, a closure is a technique for implementing lexically scoped named binding. It is a way of storing a function with an environment.
108107

109-
```js
110-
const addTo = (x) => {
111-
return (y) => {
112-
return x + y
113-
}
114-
}
115-
```
108+
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.
109+
ie. they allow referencing a scope after the block in which the variables were declared has finished executing.
116110

117-
We can call `addTo` with a number and get back a function with a baked-in `x`.
118111

119112
```js
120-
var addToFive = addTo(5)
113+
const addTo = x => y => x + y;
114+
var addToFive = addTo(5);
115+
addToFive(3); //returns 8
121116
```
117+
The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5.
118+
119+
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.
122120

123-
In this case the `x` is retained in `addToFive`'s closure with the value `5`. We can then call `addToFive` with the `y`
124-
and get back the desired number.
121+
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.
125122

126-
```
127-
addToFive(3) // => 8
128-
```
123+
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).
129124

130-
This works because variables that are in parent scopes are not garbage-collected as long as the function itself is retained.
125+
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.
131126

132-
Closures are commonly used in event handlers so that they still have access to variables defined in their parents when they
133-
are eventually called.
127+
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.
134128

135-
__Further reading__
129+
__Further reading/Sources__
136130
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
137-
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
138-
131+
* [JavaScript Closures highly voted discussion](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
139132

140133
## Partial Application
141134

@@ -188,37 +181,6 @@ add2(10) // 12
188181

189182
```
190183

191-
## Closure
192-
193-
A closure is a way of accessing a variable outside its scope.
194-
Formally, a closure is a technique for implementing lexically scoped named binding. It is a way of storing a function with an environment.
195-
196-
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.
197-
ie. they allow referencing a scope after the block in which the variables were declared has finished executing.
198-
199-
200-
```js
201-
const addTo = x => y => x + y;
202-
var addToFive = addTo(5);
203-
addToFive(3); //returns 8
204-
```
205-
The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5.
206-
207-
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.
208-
209-
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.
210-
211-
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).
212-
213-
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.
214-
215-
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.
216-
217-
218-
__Further reading/Sources__
219-
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
220-
* [JavaScript Closures highly voted discussion](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
221-
222184
## Auto Currying
223185
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.
224186

0 commit comments

Comments
 (0)