A Week with Elixir
joearms.github.io
joearms.github.io
In any language design, the total time spent discussing
a feature in this list is proportional to two raised to
the power of its position.
0. Semantics
1. Syntax
2. Lexical syntax
3. Lexical syntax of comments
http://www.haskell.org/haskellwiki/Wadler's_Law js> a = 5;
5
js> f = function(x) { return x+a };
function (x){return x+a}
js> f(10)
15
js> a = 100
100
js> f(10)
110
Well, yes, this is true, but it's not a problem with JS' closures, it's a problem with its variable declaration. a is declared as a global variable by default. Yes, this is bad, and everyone knows it's bad, but it's a bit orthogonal to closures. It's very simple to solve this in JS: js> a = 5;
5
js> f = function (x) { var a = 5; return x + a };
function(x){var a;return x+a}
js> f(10)
15
js> a = 100
100
js> f(10)
15Your example doesn't really demonstrate the eponymous "closing" aspect of the closure and doesn't solve the problem that the OP is calling out. Closures are functions that refer to non-local variables. [1] [2] Whether the language provides lexical or dynamic scoping [3] is critical to how and when closures can be used in that language.
[1] http://en.wikipedia.org/wiki/Closure_(computer_science) [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid... [3] http://en.wikipedia.org/wiki/Scope_(computer_science)#Lexica...
It's really exciting to see functional programming beginning to be recognized by more and more developers. It looks like Elixir will be a great language to help that trend grow.