Skip to content

Commit

Permalink
Simplified closure definition hemanth#162
Browse files Browse the repository at this point in the history
  • Loading branch information
jethro larson committed Mar 20, 2018
1 parent b10eba9 commit aeac2ee
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ __Table of Contents__

* [Arity](#arity)
* [Higher-Order Functions (HOF)](#higher-order-functions-hof)
* [Closure](#closure)
* [Partial Application](#partial-application)
* [Currying](#currying)
* [Closure](#closure)
* [Auto Currying](#auto-currying)
* [Function Composition](#function-composition)
* [Continuation](#continuation)
Expand Down Expand Up @@ -94,6 +94,43 @@ const is = (type) => (x) => Object(x) instanceof type
filter(is(Number), [0, '1', 2, null]) // [0, 2]
```

## Closure

A closure is a scope which retains variables available to a function when it's created. This is important for
[partial application](#partial-application) to work.


```js
const addTo = (x) => {
return (y) => {
return x + y
}
}
```

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

```js
var addToFive = addTo(5)
```

In this case the `x` is retained in `addToFive`'s closure with the value `5`. We can then call `addToFive` with the `y`
and get back the desired number.

```
addToFive(3) // => 8
```

This works because variables that are in parent scopes are not garbage-collected as long as the function itself is retained.

Closures are commonly used in event handlers so that they still have access to variables defined in their parents when they
are eventually called.

__Further reading__
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)


## Partial Application

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

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.


## Currying

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

```

## Closure

A closure is a way of accessing a variable outside its scope.
Formally, a closure is a technique for implementing lexically scoped named binding. It is a way of storing a function with an environment.

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.
ie. they allow referencing a scope after the block in which the variables were declared has finished executing.


```js
const addTo = x => y => x + y
var addToFive = addTo(5)
addToFive(3) // returns 8
```

The function `addTo()` returns a function(internally called `add()`), lets store it in a variable called `addToFive` with a curried call having parameter 5.

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.

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.

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).

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.

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.


__Further reading/Sources__
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
* [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)

## Auto Currying
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.

Expand Down

0 comments on commit aeac2ee

Please sign in to comment.