Oddly enough, I think Morendil would get a real kick out of JavaScript. So much in JS involves passing functions around, usually carrying around some variables from their enclosing scope. That’s how the OO works; it’s how you make callbacks seem natural; it even lets you define new control-flow structures like jQuery’s each() function, which lets you pass in a function which iterates over every element in a collection.
The clearest, most concise book on this is Doug Crockford’s Javascript: The Good Parts. Highly recommended.
The technical term for this is a closure. A closure is a first-class* function with some associated state. For example, in Scheme, here is a function which returns counters, each with its own internal ticker:
While we’re sharing fun information, I’d like to point out a little-used feature of Markdown syntax: if you put four spaces before a line, it’s treated as code. Behold:
Oddly enough, I think Morendil would get a real kick out of JavaScript. So much in JS involves passing functions around, usually carrying around some variables from their enclosing scope. That’s how the OO works; it’s how you make callbacks seem natural; it even lets you define new control-flow structures like jQuery’s each() function, which lets you pass in a function which iterates over every element in a collection.
The clearest, most concise book on this is Doug Crockford’s Javascript: The Good Parts. Highly recommended.
The technical term for this is a closure. A closure is a first-class* function with some associated state. For example, in Scheme, here is a function which returns counters, each with its own internal ticker:
To create a counter, you’d do something like
Then, to get values from the counter, you could call something like
Here is the same example in Python, since that’s what most people seem to be posting in:
*That is, a function which you can pass around like a value.
While we’re sharing fun information, I’d like to point out a little-used feature of Markdown syntax: if you put four spaces before a line, it’s treated as code. Behold:
Also, the emacs rectangle editing functions are good for this. C-x r t is a godsend.