Live data from Hacker News

What's a Closure?

nathansjslessons.appspot.com

31–40 of 60 posts

Re: What's a Closure?

#31
post #3

Maybe somebody could explain what the difference between a lambda, a closure and a monad is, and how/if they are different from a function pointer (or generalization of it like a signal), or from unary or binary function objects. (my C++ bias in asking this question may be obvious ;) ) I see the first three concepts used interchangeably (as far as I understand), but maybe that's because they're used slightly differen…

I see several explanations here already, none of which gets at the essence of the difference between a lambda expression and a closure. A lambda expression (or juat "lambda" for short) is a syntactic construct: something you write in your program, like "(lambda (x) (+ x 1))" or "function (x) { return x + 1; }". Lambda expressions, like any other evaluable expressions, have values . Just as the expression "2 + 2" has…

Very informative, thank you!

Re: What's a Closure?

#32
post #13

This was a clever use of unit tests to help you "pass" each lesson. I've read a bunch of tutorials on closures, but this one really helped me "get" it. Sure, it takes liberties and glosses over things... but seeing it grow right before my eyes was really helpful.

Agreed! It would have been even more helpful if the unit tests showed both the expected result and the actual result. Now I had to guess at what the problem might be (which led to me giving up on the last problem).

Re: What's a Closure?

#35
My C++ brain must be getting too old -- head is swimming :( From all the talk, these must be very useful, but I don't understand why? Why not just use an object?

Re: What's a Closure?

#36
post #19

Wow, I love the flow! Except JSLint. JSLint barfs all over otherwise valid JavaScript and provides really unhelpful error messages. For a minute, I thought that I forgot how to write valid JavaScript.... Hate! Hate! Hate! You're teaching folks to program, not write syntactically pure JS. But drop that and the experience is great.

(Creator here) Ha, yeah JSLint really provokes strong emotions. Before I added in JSLint, if you misplaced one semicolon or forgot a parentheses, none of the tests would run and it would just say "program failed". That was incredibly frustrating to me. I figured that if I was getting frustrated, and I was the creator of the tests, then random users would be REALLY frustrated. So I added in JSLint. Now you get line an…

JSLint complained when I had two `var` statements; it wanted me to merge the two into one. I repeat `var`, so this annoyed me. (Of course, it wasn't hard to fix, but the examples didn't show any multi-`var` usage so it may be a problem for someone new to the language or programming in general.)

Re: What's a Closure?

#37
Anyone else try to do a lazy AND evaluation for chapter 12 and have it fail? I did

fC(gC(function(){success,failure};),failure);

but the desired answer checked gC even if fC already failed..

Re: What's a Closure?

#38
post #3

Maybe somebody could explain what the difference between a lambda, a closure and a monad is, and how/if they are different from a function pointer (or generalization of it like a signal), or from unary or binary function objects. (my C++ bias in asking this question may be obvious ;) ) I see the first three concepts used interchangeably (as far as I understand), but maybe that's because they're used slightly differen…

I see several explanations here already, none of which gets at the essence of the difference between a lambda expression and a closure. A lambda expression (or juat "lambda" for short) is a syntactic construct: something you write in your program, like "(lambda (x) (+ x 1))" or "function (x) { return x + 1; }". Lambda expressions, like any other evaluable expressions, have values . Just as the expression "2 + 2" has…

There are two kinds of lambda expressions: combinators, which only combine their arguments and make no reference to any other variables; and open functions, which refer to variables that are not bound by the lambda expression.

A closure is an open function paired with an environment that maps the unbound variables to values. The pairing is called a closed function, or the closure of an open function.

Re: What's a Closure?

#39
post #3

Maybe somebody could explain what the difference between a lambda, a closure and a monad is, and how/if they are different from a function pointer (or generalization of it like a signal), or from unary or binary function objects. (my C++ bias in asking this question may be obvious ;) ) I see the first three concepts used interchangeably (as far as I understand), but maybe that's because they're used slightly differen…

I see the first three concepts used interchangeably (as far as I understand)... Either you misunderstood, or the people using them misunderstood. Lambdas are anonymous functions. In javascript terminology: _.map(someNumbers, function (x) { return x +1; } /* Closures are a way of hiding state (or at least data) in anonymous functions: makeCounter() { var x = 0; function numCalls() { return x++; }; Monads are a method…

And it is important to remember that all functions in JavaScript can (and some say "should") be defined this way.

    var c = 2;
    var f = function (x) { return (2 * x); };
    var g = function (h, x) { return h(x); };

Re: What's a Closure?

#40
post #37

Anyone else try to do a lazy AND evaluation for chapter 12 and have it fail? I did fC(gC(function(){success,failure};),failure); but the desired answer checked gC even if fC already failed..

My first attempt was about the same, until I realized I missed a requirement:

> Your function bothC should call both fC and gC no matter what

What happens in your code if fC fails?

Post reply on HN