Skip to content

Instantly share code, notes, and snippets.

@pbouchet
Created May 23, 2012 20:57
Show Gist options
  • Save pbouchet/2777770 to your computer and use it in GitHub Desktop.
Save pbouchet/2777770 to your computer and use it in GitHub Desktop.
Closures
** Scheme **
(define makeaccount
(lambda (balance)
(lambda (amt)
(begin (set! balance (+ balance amt))
balance))))
<==>
** JavaScript **
var makeAccount = function(balance) {
return function(amt) {
balance += amt; // since balance is in the local scope, this function will close around it
return balance; // even though it hasn't been assigned to a var (see jsfiddle.net/6g5U6).
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment