Live data from Hacker News

What's a Closure?

nathansjslessons.appspot.com

51–60 of 60 posts

Re: What's a Closure?

#51
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…

Use JSHint with

  /*jshint asi:true, eqnull:true, laxbreak: true */
and you'll make everyone happy :)

Re: What's a Closure?

#52
post #23

Earlier quoted context omitted.

(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…

Do you know a way to disable strict identity over equality checking (=== vs ==)? It's a Crockford-ism, but I think the jury's still out on the value of strict type checking vs coercion and I'd like the choice.

  eqeq: true
see http://www.jslint.com/lint.html

Re: What's a Closure?

#53
post #26

Sorry if this is a stupid question but when you use continuations-passing style, what happens to the stack in javascript? Does it clean it self up or does the language just keeps going functions all the way down and fills up?

If you're doing CPS in a language with tail-call optimization then the same stack frame will be used for all your function calls. I don't know if V8 does this. In something like Node you don't do full CPS, you have portions of your code in CPS but usually only a few layers deep as you need to give control back to the event loop at some point.

I don't think JS has tail-call optimization. At some point you'll likely hit an error similar to too much recursion, but it would require a scope chain in the thousands.

Re: What's a Closure?

#54
post #17

It would be awesome if the web version of LPTHW had an interactive tool just like this.

Agreed. Once I converted all the exercises to coffeescript (syntactically similar to python) they made more sense.

Re: What's a Closure?

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

I had the same problem, and these JavaScripts even don't allow for alert, to see the variable value.

Re: What's a Closure?

#56
post #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?

Many a brain have pondered the same question since the beginning of time. For instance, you can meditate over the following koan:

-----

The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."

Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.

On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.

-----

Anton van Straaten in http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/m...

Re: What's a Closure?

#57
post #42

Earlier quoted context omitted.

(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…

SPOILERS I created a successful bothC function that looks more like the simplified version of seqC, calling fC first. But when I switch to calling gC first, I get the following failures. Am I missing something, or is this a bug in the unit test? PASSED No JSLint errors PASSED bothC(S, S, A, B) === undefined PASSED output === "SSA" PASSED bothC(S, F, A, B) === undefined FAILED output === "SSASFB" PASSED bothC(F, S, A,…

I'm not sure that we used the same method to get to this part, but I too ran into this output.

Interested in seeing how my solution was off, and if I was thinking about it the wrong way. Any luck?

Re: What's a Closure?

#58
post #57
post #42

Earlier quoted context omitted.

SPOILERS I created a successful bothC function that looks more like the simplified version of seqC, calling fC first. But when I switch to calling gC first, I get the following failures. Am I missing something, or is this a bug in the unit test? PASSED No JSLint errors PASSED bothC(S, S, A, B) === undefined PASSED output === "SSA" PASSED bothC(S, F, A, B) === undefined FAILED output === "SSASFB" PASSED bothC(F, S, A,…

I'm not sure that we used the same method to get to this part, but I too ran into this output. Interested in seeing how my solution was off, and if I was thinking about it the wrong way. Any luck?

Continuation Passing SPOILERS:

f_success = function() { gC(success, failure); }; f_failure = function() { gC(failure, failure); }; fC(f_success, f_failure);

For me, the trick here was to forget about the words "success" and "failure" because they have too much semantics embedded. I had to re-word the problem like this: call f3, if first argument of f1 and f2 are called. All other roads lead to f4. Both f1 and f2 has to be called in the chain no matter what.

Also, this is the first time I've seen such a pattern. Is there a practical application of such pattern? I think it's ok to have a single level of closure to continue execution, but if it goes to that many levels like the sample problem, it'll be a nightmare to maintain for people who have not seen your code yet.

Re: What's a Closure?

#59
post #15
post #14

is it me or this is confusing? "Define a function named generate that takes one argument, a function f. It should return an array that consists of the function f and another function that doubles its input."

Define a function var ... = function( ... ){ ... }; named generate var generate = function( ... ){ ... }; that takes one argument, a function f var generate = function( f ){ ... }; It should return an array var generate = function( f ){ return [ ... ]; }; that consists of the function f var generate = function( f ){ return [ f, ... ]; }; and another function var generate = function( f ){ return [ f, function( ... ){…

Your Answer is superb but can you tell me what is wrong with this

var generate = function (f) {

   var double = function (x){return x * 2;};
    
   var list = [f,double];
    return list;
};

On the other hand this one passes the JSLInt test given here at : http://nathansjslessons.appspot.com/lesson?id=1050

var generate = function (f) {

    function double(x){return x * 2;}
    
    var list = [f,double];
     return list;
    
};

Re: What's a Closure?

#60
post #15

Earlier quoted context omitted.

Define a function var ... = function( ... ){ ... }; named generate var generate = function( ... ){ ... }; that takes one argument, a function f var generate = function( f ){ ... }; It should return an array var generate = function( f ){ return [ ... ]; }; that consists of the function f var generate = function( f ){ return [ f, ... ]; }; and another function var generate = function( f ){ return [ f, function( ... ){…

Your Answer is superb but can you tell me what is wrong with this var generate = function (f) { var double = function (x){return x * 2;}; var list = [f,double]; return list; }; On the other hand this one passes the JSLInt test given here at : http://nathansjslessons.appspot.com/lesson?id=1050 var generate = function (f) { function double(x){return x * 2;} var list = [f,double]; return list; };

I'm sorry I can't. I'm not into the details of Javascript (yet) and know nothing about JSLint.
Post reply on HN