From d1f6c16a92ba13767fd6b1dcbbb76381cfd35f32 Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Fri, 9 Sep 2016 13:30:41 +0530 Subject: [PATCH 1/6] Adding Closure --- readme.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/readme.md b/readme.md index 57b0296..7bda73d 100644 --- a/readme.md +++ b/readme.md @@ -18,6 +18,7 @@ __Table of Contents__ * [Higher-Order Functions (HOF)](#higher-order-functions-hof) * [Partial Application](#partial-application) * [Currying](#currying) +* [Closure](#closure) * [Auto Currying](#auto-currying) * [Function Composition](#function-composition) * [Purity](#purity) @@ -144,6 +145,35 @@ const add2 = curriedSum(2) // (b) => 2 + b add2(10) // 12 ``` +##Closure + +A very simplistic definition - A closure is a way of accessing a variable outside its scope. +Formally, a closure is a technique for implementing lexically scopped 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 +function getTicker () { + var tick = 0; + function ticker () { + tick = tick + 1; + return tick; + } + return ticker; + } + var tickTock = getTicker(); + tickTock(); //returns 1 + tickTock(); //returns 2 +``` +The function getTimer() returns a function(internally called ticker), lets call it tickTock. + +Ideally, when the getTimer finishes execution, its scope, with local variable tick should also not be accessible. But, it returns 1, 2, 3.. on calling tickTock(). This simply means that, somewhere it keeps a track of the variable tick. + +Lexical scoping is the reason why it is able to find the value of tick, the private variable 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 and upon re-execution same stack is restored. + + ## 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. From e0ae226d99c3808011160eb51b1a6ca7b973b359 Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Fri, 9 Sep 2016 13:34:17 +0530 Subject: [PATCH 2/6] Fixed typos in Closure --- readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 7bda73d..03ed7d6 100644 --- a/readme.md +++ b/readme.md @@ -145,6 +145,7 @@ const add2 = curriedSum(2) // (b) => 2 + b add2(10) // 12 ``` + ##Closure A very simplistic definition - A closure is a way of accessing a variable outside its scope. @@ -167,13 +168,11 @@ function getTicker () { tickTock(); //returns 1 tickTock(); //returns 2 ``` -The function getTimer() returns a function(internally called ticker), lets call it tickTock. +The function getTimer() returns a function(internally called ticker), lets store it in a variabke called tickTock. Ideally, when the getTimer finishes execution, its scope, with local variable tick should also not be accessible. But, it returns 1, 2, 3.. on calling tickTock(). This simply means that, somewhere it keeps a track of the variable tick. -Lexical scoping is the reason why it is able to find the value of tick, the private variable 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 and upon re-execution same stack is restored. - - +Lexical scoping is the reason why it is able to find the value of tick - the private variable 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 and upon re-execution same stack is restored. ## 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. From a5eaf03818a621730a0c921c3d719ba185b43e03 Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Fri, 9 Sep 2016 13:35:49 +0530 Subject: [PATCH 3/6] Fixed typos in Closure --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 03ed7d6..fa277f3 100644 --- a/readme.md +++ b/readme.md @@ -168,9 +168,9 @@ function getTicker () { tickTock(); //returns 1 tickTock(); //returns 2 ``` -The function getTimer() returns a function(internally called ticker), lets store it in a variabke called tickTock. +The function getTicker() returns a function(internally called ticker), lets store it in a variabke called tickTock. -Ideally, when the getTimer finishes execution, its scope, with local variable tick should also not be accessible. But, it returns 1, 2, 3.. on calling tickTock(). This simply means that, somewhere it keeps a track of the variable tick. +Ideally, when the function getTicker finishes execution, its scope, with local variable tick should not be accessible. But, it returns 1, 2, 3.. on calling tickTock(). This simply means that, somewhere it keeps a track of the variable tick. Lexical scoping is the reason why it is able to find the value of tick - the private variable 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 and upon re-execution same stack is restored. From 9d14bcf9433ec83dd97ff94db3119a662a7881a1 Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Sun, 18 Sep 2016 00:22:40 +0530 Subject: [PATCH 4/6] Update readme.md --- readme.md | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/readme.md b/readme.md index fa277f3..059a0e4 100644 --- a/readme.md +++ b/readme.md @@ -148,7 +148,7 @@ add2(10) // 12 ##Closure -A very simplistic definition - A closure is a way of accessing a variable outside its scope. +A closure is a way of accessing a variable outside its scope. Formally, a closure is a technique for implementing lexically scopped 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. @@ -156,23 +156,22 @@ ie. they allow referencing a scope after the block in which the variables were d ```js -function getTicker () { - var tick = 0; - function ticker () { - tick = tick + 1; - return tick; - } - return ticker; - } - var tickTock = getTicker(); - tickTock(); //returns 1 - tickTock(); //returns 2 +var addTo = function(x) { + function add(y) { + return x + y; + } + return add; +} +var addToFive = addTo(5); +addToFive(3); //returns 8 ``` -The function getTicker() returns a function(internally called ticker), lets store it in a variabke called tickTock. +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 getTicker finishes execution, its scope, with local variable tick should not be accessible. But, it returns 1, 2, 3.. on calling tickTock(). This simply means that, somewhere it keeps a track of the variable tick. +Ideally, when the function ```addoTo``` 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 value of tick - the private variable 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 and upon re-execution same stack is restored. +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). ## 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. From b01d0f6cfcb4052ce7e64d6e2762d1540385baa9 Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Sun, 9 Apr 2017 10:54:48 +0530 Subject: [PATCH 5/6] Update readme.md Added closure vs lambda as suggested. --- readme.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 059a0e4..7c7f5bd 100644 --- a/readme.md +++ b/readme.md @@ -146,7 +146,7 @@ add2(10) // 12 ``` -##Closure +## Closure A closure is a way of accessing a variable outside its scope. Formally, a closure is a technique for implementing lexically scopped named binding. It is a way of storing a function with an environment. @@ -156,23 +156,26 @@ ie. they allow referencing a scope after the block in which the variables were d ```js -var addTo = function(x) { - function add(y) { - return x + y; - } - return add; -} +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 ```addoTo``` 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. +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) +* [JavaScript Closures highly voted disucussion](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. From a07df820ca7b8b1a9ea3bd21e9ffdca7a219c3ee Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Sun, 9 Apr 2017 11:25:21 +0530 Subject: [PATCH 6/6] Update readme.md --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 7c7f5bd..36ff36e 100644 --- a/readme.md +++ b/readme.md @@ -169,6 +169,7 @@ Lexical scoping is the reason why it is able to find the values of x and add - t 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.