lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You may also want a more restrictive definition of lambda functions: it would be a functions which makes no access at all to its environment (i.e. where its closure contains no references to other external variables), for example:   function(x) return x^2 end

Le dim. 18 nov. 2018 à 20:03, Philippe Verdy <verdy_p@wanadoo.fr> a écrit :
Lambda functions in Lua are just standard functions which are not bound to a name, i.e. expressions of the form "function() ... end" or  "function(parameters...) ... end"

You can declare a function with a bound name by inserting it between "function" and the next "(", in which case it will be also bound to a new local variable (or will replace its value if you have a local variable); it does not replace a function or variable with identical name in the current outer scope.

You can also bind a local function to give it as the value of an existing local or outer variable (if there's none, i.e. this variable name has a nil value) then a new local varaible will be created, otherwise the lambda function will overwrite the value of the existing variable.

So "function f(x)... end" is not completely equivalent to "f= function(x) ... end": the first one declares a new local variable named "f", the second overwrites an existing variable "f" in scope, or creates a new local variable.

Lua not  really know if your function is named or not: it may have several names (given as different keys of the same table, or different or identical names in distinct tables). It always runs lambda functions.

When debugging Lua programs, Lua will attempt to locate the name of the function by performing lookups in the stack of closures, to see if there environment defines a table of variable names bound to the function defined for the closure: if it locate a table, it will then scan all keys on this table to see if one of the keys is mapped to the function.

You may initially create a local function bound to a local name, then assign it to another variable in scope with another name, then when the local variable is no longer in scope or reassigned another value, that function will no longer be bound to its initial name. So a function may have its "most local" name changed over time. function names are not significant for their execution, they are interesting only for debuggers. What only matters are the anonymous lambda functions that every function has.

Note that all lambda functions are not just a series of instructions: they also have a closure in which they are defined (that closure may not even have any external variable in rare extreme cases where access to extertnal variables was blocked by a function set in the closure's metatable "__index" set to a function that is always returning nil, and all what your lambda function will have access to are the upvalues in the call stack, where the values of function unput parameters are passed and where the function can return its output results)


Le dim. 18 nov. 2018 à 19:37, Dirk Laurie <dirk.laurie@gmail.com> a écrit :
Please refresh my memory!

There have been numerous discussions on this list for lambda
notations. Were any of those accompanied by a patch so that one could
try them out?