22 ms·
Pierce ... coined “open recursion” to refer to the kind of extensions you need to build an OOP language from a non-OOP one that just has functions (i.e. “lambda
by Animats 9y ago
Pierce ... coined “open recursion” to refer to the kind of extensions you need to build an OOP language from a non-OOP one that just has functions (i.e. “lambdas”, “closures”, or “anonymous delegates”) and records (more or less “object literals” in JS or “maps” in other languages).
Which has nothing to do with recursion.
A lambda and a closure are completely different things. A lambda is just an anonymous function. A closure is a function instance which carries with it some context from an outer scope. You can have a lambda which is not a closure, and a closure which is not a lambda. Javascript often has closures which are not lambdas - that's the usual form of a callback function.
For languages that work much like LISP, with dynamic function creation, nested functions, and garbage collection, you get most of the heavy machinery needed for closures by default. If you've got that, you can kludge objects into existence. This was originally called "flavors" in LISP. Javascript does this, and suffers from having too many ways to create OOP-like objects. The LISP and Javascript experiences indicate that implementing OOP via closures creates a mess in source code.
Lambdas are just syntactic sugar for nested functions.
- noelwelsh 9y agoSome communities distinguish between lambdas and closures, but some don't. In the (mainstream) functional programming community it is unusual to make this distinction. The recursion in open recursion refers to the ability of an object to refer to itself (the 'this' or 'self' value) and the open bit refers to late binding (the value to which 'this' or 'self' is bound is open to change). Agreed on the duality between closures and objects. Closures are a poor man's objects and objects are a poor man's closures, as the joke goes.
- the_common_man 9y ago> Javascript often has closures which are not lambdas - that's the usual form of a callback function. Can you clarify this further? To me a callback function is a lambda. It may or may not be a closure.
- saghm 9y agoIt seems like GP might have gotten it backwards; the usual form of a callback is a lambda that's not a closure. However, a callback function might not be either, e.g.: function foo(x) { return x + 1; } function bar(f, y) { return f(y); } // `foo` is a callback but neither a lambda nor a closure bar(foo, 2); edit: adding the missing the "b" in lambda
- the_common_man 9y agoYeah, your explanation is consistent with my understanding.
- sillysaurus3 9y agofoo and bar are both lambdas and closures. A named lambda is still a lambda. foo = function (x) { return x + 1; } is identical to function foo (x) { return x + 1; } They are also closures. Since they don't refer to any outer variables, they are closures containing zero context variables. If foo were to refer to some other variable, the closure would contain one context variable. The only reason to distinguish between lambdas and closures is because some languages do not support closures (e.g. early versions of Emacs Lisp). Attempting to refer to other variables would always refer to a global variable with that name.
- saghm 9y ago> foo and bar are both lambdas and closures. A named lambda is still a lambda. > foo = function (x) { return x + 1; } > is identical to > function foo (x) { return x + 1; } I'll concede that for JavaScript (and a few other languages); some languages don't treat all functions as just a lambda with a binding, though. > They are also closures. Since they don't refer to any outer variables, they are closures containing zero context variables. If foo were to refer to some other variable, the closure would contain one context variable. I'm not very keen on the definition of "closure" including functions that close over zero variables, because then "closure" becomes indistinguishable from "function", and the word is no longer useful. I might be wrong about the technical definition, but I feel like having a word for closures isn't very useful if it doesn't mean "closing over one or more variables".
- wickawic 9y ago> Which has nothing to do with recursion. Had you read the article you might have realized this term is overloaded.
- Animats 9y ago“When I use a word,” Humpty Dumpty said, in rather a scornful tone, “it means just what I choose it to mean—neither more nor less.” “The question is,” said Alice, “whether you can make words mean so many different things.” “The question is,” said Humpty Dumpty, “which is to be master—that's all.” - Lewis Carroll, a mathematician who wrote stories in his spare time.
- pasquinelli 9y agoA function calls a function with its own name-- there is a resemblance to recursion, where a function calls a function with its own name. This is the first I've ever heard of any controversy with the term "open recursion." Is it a common complaint, that the term "open recursion" abuses the notion of recursion?
- Tarean 9y agoJust because you didn't happen to not know this one doesn't mean that anyone just invented a new meaning. https://en.m.wikipedia.org/wiki/This_(computer_programming)#Open_recursion https://en.m.wikipedia.org/wiki/This_(computer_programming)#...
- dTal 9y agoI feel compelled to link the xkcd from the Friday before last: https://xkcd.com/1860/ https://xkcd.com/1860/