3 ms·
There's a lot of confusion about this. A lambda is just a function without a name. (This feature tends to come with special syntax, although it doesn't have to
by Animats 7y ago
There's a lot of confusion about this.
A lambda is just a function without a name. (This feature tends to come with special syntax, although it doesn't have to.)
A nested function is a function defined inside another function which can access the variables of the enclosing function. (A nested function can be a lambda, but it doesn't have to be. Some languages have named nested functions. A lambda doesn't have to be a nested function; it doesn't have to pull in any variables from an outer scope.)
A closure is a nested function which can outlive the outer function, keeping its data alive. (A closure can be named, the usual case in Python and Javascript, or anonymous. So a closure need not be a lambda.)
Closures are easy to implement in garbage collected languages, but hard in explicitly allocated ones, because extending the lifetime of the imported data gets complicated.
So the options are:
- Lambda without any external data access -- typical use, comparison function for a sort.
- Lambda with external data access, but not outliving its enclosing function - typical use, iteration expression
- Named function with no external data access. Typical use, a local function in languages that don't do local functions well, such as C.
- Named function with external data access, not outliving its enclosing function. Typical use, internal function within a function to avoid passing extra parameters.
- Named function with external data access, outliving its enclosing function. A true closure, but not a lambda. Typical use, saving state for a callback in Javascript by passing the function to something that will save it and invoke it later. An object, really. This was how LISP did objects.
- Lambda function with external data access, outliving its enclosing function. A true closure. Same uses as above, but in different languages.
Most languages offer some subset of these six options.
- deleted 7y ago[deleted]
- jarjoura 7y agoI think the OP makes it clear that this nuance is actually quite tricky in a native systems language like Rust. A good example in C++ would be: - A function (non capturing/lambda) is created with auto f = [] { ... }; - A function (capturing/closure) is created with auto s = "..."; auto f = [&] { s; ... }; In source code both of those look similar and you can even define them with the same type: std::function<void()> However, the resulting assembly for both couldn't be more different. The Lambda case is as raw as any normal C function type, which the closure case creates a C++ class that closes over the outer method's state. If you're not a seasoned C++ engineer, this nuance will be lost on you. It's fair to say that everyone should understand Lambda Calculus rules, but that makes the language less accessible to new-comers. In the case for non-systems languages, you can blur the lines with ease, but something Rust and C++ just cannot afford to do. That makes understanding the nuance important to be effective.
- vlovich123 7y agoI’m pretty sure that the generated assembly for both your examples written as-is is the same. The optimizer should see through the lambda sugar unless the example gets weird or you type-erase via std::function. I think your broader point still holds but perhaps could do with a clearer example