hemanth / functional-programming-jargon

Jargon from the functional programming world in simple terms!
http://git.io/fp-jargons
MIT License
18.58k stars 1.02k forks source link

Explanation of Closure is too complicated #162

Closed afayes closed 6 years ago

afayes commented 7 years ago

The explanation of closure uses currying to explain it. The explanation could be far simpler without the use of currying. For people not aquatinted with Function programming, its adds unnecessary complexity to a simple concept.

Here is a simpler example: var a = 4; function myFunction() { return a * a; }

wilmarques commented 7 years ago

I think it doesn't describe very well why closures. It seems more a example of how scopes work.

afayes commented 7 years ago

@wileymarques Once a simple example is used to show what a closure is, you could then explain 'why' and maybe show a more a more realistic example.

SeanSilke commented 7 years ago

@afayes you can also enclosed myFunction in other function that then return myFunction. To show that function can enclosed scope of function that already return. That will explain why closures as @wileymarques mention. @wileymarques afayes present decent closure. In the end Clojure is for closing up scopes.

afayes commented 7 years ago

One example of 'why' you would use closure in JS is to create private static variables in a class .e.g.

var Person = (function() { // private static variable var counter = 0;

// closure
return function() {
    counter++;
    this.name = "blah";
    alert(counter);
}

})();

However this example is not the simplest to understand. I'm sure there are simpler usages.

wilmarques commented 7 years ago

So maybe just an example not using ES6?

Something like this?

function createAdd10Closure() {
    const x = 10;

    function add10(number) {
        return numero + x;
    }

    return add10;
}

const add10 = createAdd10Closure();
add10(5); // 15
afayes commented 7 years ago

@wileymarques Your example is easy to follow

jethrolarson commented 7 years ago

It might be easy to follow but it's equivalent to the simpler function:

const add10 = x => 10 + x

So I don't think it shows as much value.

Also, since this repo is about functional programming I wouldn't use mutable state as an example and the iife adds it's own complexity.

Maybe the concept could be explained with the global variable but that kind of makes me wince

jethrolarson commented 7 years ago

Again this isn't a guide to javascript or the general concept of closures but to what they mean in a functional context

stereobooster commented 7 years ago

Fundamental Concepts in Programming Languages, STRACHEY, 1967:

Thus the R-value of a function contains two parts — a rule for evaluating the expression, and an environment which supplies its free variables. An R-value of this sort will be called a closure. The most straightforward way of representing the environment part is by a pointer to a Free Variable List (FVL) which has an entry for each free variable of the function.

An L-value represents an area of the store of the computer. We call this a location rather than an address in order to avoid confusion with the normal store-addressing mechanism of the computer.

The two essential features of a location are that it has a content—i.e. an associated R-value — and that it is in general possible to change this content by a suitable updating operation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures:

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

andys8 commented 7 years ago

I think the example should be improved. I can't really understand the first sentence of the explanation:

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

Why is it "called add() internally"? There is no add in the example. Is it referencing the + operator or is the code incomplete or is the explanation wrong?

wilmarques commented 7 years ago

It might be easy to follow but it's equivalent to the simpler function:

const add10 = x => 10 + x

@jethrolarson I think the problem here is that people doesn't understand Arrow Functions yet.

jethrolarson commented 7 years ago

That may be a hurdle but I don't think that's a problem that this guide should look to fix.

On Mon, Aug 7, 2017 at 5:52 AM Wiley Marques notifications@github.com wrote:

It might be easy to follow but it's equivalent to the simpler function:

const add10 = x => 10 + x

@jethrolarson https://github.com/jethrolarson I think the problem here is that people doesn't understand Arrow Functions yet.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/hemanth/functional-programming-jargon/issues/162#issuecomment-320655079, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB-4FM0nd54yBWlkx_EQjAztH-hJ9C8ks5sVwiIgaJpZM4OOkof .

wilmarques commented 7 years ago

@jethrolarson I agree. I'm just pointing the problem.

Maybe some advice on the top of the document to learn ES6/x first would help.

jethrolarson commented 7 years ago

There's this bit at the top of the document:

Examples are presented in JavaScript (ES2015). Why JavaScript?

The link points to this page https://github.com/hemanth/functional-programming-jargon/wiki/Why-JavaScript%3F#whats-with-the-

jethrolarson commented 6 years ago

I took a whack at simplifying the current definition. Give the PR a look if you can.

jethrolarson commented 6 years ago

My change to address this issue has been merged. Feel free to reopen if you think it's not good enough