What's a Closure?
nathansjslessons.appspot.com
What's a Closure?
1–10 of 60 posts
Re: What's a Closure?
#2If someone already knows some basic javascript, you could show them a basic, familiar function but then show them how it can become a closure. Then make them realize that everything can be treated essentially as an object in javascript, nearly everything, but especially functions.
I didn't know the formal names of "Stateful Closures" or "Continuation Passing" but I knew that I can make a copy of a function as another object so it retains different values than another copy and that I can pass a function as a return value.
They should also cover self-executing closures and using timers in closures via setTimeout(function(){ - I don't think those concepts are too advanced.
Re: What's a Closure?
#3I see the first three concepts used interchangeably (as far as I understand), but maybe that's because they're used slightly different between different communities?
Re: What's a Closure?
#4If you already know some JavaScript you can skip the beginning and start right away at 8, "Nested Functions". Otherwise if you know any other programming language just start with the lessons and reach the lessons and learn about closures in less than 10 minutes, and -- maybe even more important -- learn many of JavaScript's basic on the way.
I really hope the author of this does many more lessons. I found this to be a really great way to introduce some of JavaScript's somewhat awkward features, like local variables and function scope.
Compared to Eloquent Javascript that also features try-as-you-read exercises I found this site to be more condensed. Decide for your own whether this is an advantage or a disadvantage. I'd suggest taking the best of both worlds, use this to get the basics and see if you get a hang on JS, and if yes continue with the examples from Eloquent Javascript.
Re: What's a Closure?
#5Maybe 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…
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 of encapsulation, with goals similar to object orientation. OO is about hiding data inside objects, whereas monads are about hiding most of the outside world from your functions.Re: What's a Closure?
#6Maybe 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…
A closure is a function, combined with the set of variables declared outside its definition (a 'lexical scope').
A monad has absolutely nothing to do with functions/lambdas/closures. The 5-second explanation is that it's a "design pattern" that's useful for managing control flow (often chaining operations together). But Google around a bit if you're interested.
A function-pointer/function-object in C++ are functions that don't have access to external lexically scoped variables. They can only access their arguments, or dynamically scoped (roughly 'global') variables. They are 1/2 of a closure (the missing half is the definition's lexical environment).
Re: What's a Closure?
#7Maybe 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…
A lambda is an anonymous function; it's a function that doesn't have a name. Of course you can assign it to a variable if you like to. So for example in Javascript these two are equivalent:
function foo(arg) {//function body}
foo = function(arg) {//function body}
Or in Python def foo():
return 42
foo = lambda: 42
Note that functions (and lambdas) are 'first class citizens'. This means we can pass functions around like any other type of value. You could sort of compare this to function pointers.Now a closure is related to these first class functions. It's basically a function that carries with it, the environment from when it was defined. Consider this Python/pseudocode:
def foo():
a = 42
def bar():
return a
return bar
myClosure = foo()
myClosure() #this returns 42
The function foo returns a function that we assign to myClosure (in the function foo we named it bar, but we could have used an anonymous lambda as well). The returned function returns the value of a, but note that a was only around when we defined bar. When we call myClosure, it takes the value for a from the environment when it was defined.So, lambdas and closures are not the same thing, but they're both related to first class functions. In practice when a language supports first class functions, it tends to support lambdas and closures as well.
Monads are something different again. They're used in purely functional languages (such as Haskell) to work with state. However, that's about as far as my knowledge goes so someone else can try explain that :-)
Re: What's a Closure?
#8Re: What's a Closure?
#9Maybe 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…
The List monad represents nondeterministic computation. Each possible result from the previous step in the computation is fed into the next step of the computation.
The Maybe monad represents computation that can fail. If one step of the computation fails, then the rest fail. The previous step of the computation is only fed into the next step of the computation if it succeeds.
The IO monad represents globally stateful computation. The entire state of the program is fed into each step of the computation. Kind of. That's sort of the idea, anyway - it's a model.
The Reader monad represents computation in the scope of a single shared bit of data.
Only the latter two mention state. The idea of monads in programming is just abstracting computational strategies, and sometimes those strategies require or provide state.
Monads are neat in Haskell/ML because you can write code that works on arbitrary monads. Think of it this way: you know how you can write code that operates on a `List` in Java, and it will work on `ArrayList` and `LinkedList`? Well in Haskell, you can write code that operates on/with a given function regardless of computational strategy used.
Re: What's a Closure?
#10I found that structure of teaching awkward. If someone already knows some basic javascript, you could show them a basic, familiar function but then show them how it can become a closure. Then make them realize that everything can be treated essentially as an object in javascript, nearly everything, but especially functions. I didn't know the formal names of "Stateful Closures" or "Continuation Passing" but I knew tha…
It is highly effective if you want the learner to understand a specific set of details quickly. I like it in this context.