4 ms·
If we take "Functions" as a baseline, then we have... Floating Functions / First class functions (thanks to comments for reminding): Functions that can be pass
by rdsubhas 11y ago
If we take "Functions" as a baseline, then we have...
Floating Functions / First class functions (thanks to comments for reminding): Functions that can be passed around as variables. e.g. All functions in Javascript are floating functions.
function callback() { ... }
// function assigned to a variable
var x = callback;
// function is an argument to another function
doSomething(..., callback)
Anonymous functions: functions with no name.
// an anonymous function
(function() { .... })()
// anonymous function as an argument
doSomething(..., function() { .... })
Lambda functions: A style of writing functions without a block, where the function and return value are expressed as one single expression without the usage of a block or explicit returns.
employee => employee.name
Closures: When a function references a variable outside its scope, and that "frees" the variable from its normal lifetime, thus creating a "closure" between the function and the now-free variable.
function a() {
var x = 1;
return function() {
// here a closure is created
// now "x" outlives its lifetime and
// becomes a free variable
return x + 1;
}
}
- thinkmoore 11y agoTo use academic terminology, we would refer to "floating functions" as "first class functions" (since they are not second-class citizens in the language). I would argue the true distinguishing feature of a "lambda function" in a PL is that it is first-class, nest-able, and lexically-scoped (that is, it captures bindings from its definition site). A closure is simply a way of implementing lexical scope for functions. A variable is a "free variable" not because it is "freed" from its lifetime, but because it is not bound by its enclosing lambda. (It would still be a free variable even if the function and the variable binding had the same lifetime.)
- rdsubhas 11y agoAh dang, how I missed "first class functions" :) Thanks for reminding...
- theseoafs 11y ago> Lambda functions: A style of writing functions without a block, where the function and return value are expressed as one single expression without the usage of a block or explicit returns. employee => employee.name This is not what a lambda is.