Is there a way to pass a tuple as the parameters of a Lua function?
For example, I have a function that returns multiple values
function f(a,b) return b,a end
and I want this function f to be repeatedly applied, so I can write:
f (f ... f(1,2))
But what if I need to store this initial tuple (1,2) as a variable init?
f (f ... f(init))
Is there support for this?
According to this answer, it seems python has it with the splat operator *.
Lua does not have "tuples".
When a function returns multiple values, it returns multiple values. They aren't put together into some data structure; they're separate values. If you want to store multiple return values in a single data structure, you have to actually do that.
Lua 5.2 has the table.pack function, which you can use to store multiple return values in a table. But it does so in such a way that you can decompose them later:
local values = table.pack(f(1, 2))
f(table.unpack(values, values.n))
unpack exists in Lua 5.1, but pack does not. You can emulate it easily enough:
function pack(...)
return {n = select("#", ...), ...}
end
Related
For example, I want to pass a dictionary with 10 pairs into a function to bypass the 8 valence limit. I then want the keys of the dictionary each to be assigned as a local variable to be assigned to their value and use that as the parameters for my function. Alternatively, is there a better way to pull this off?
I am afraid there is no way to functionally assign value to local scope variable. E.g.
{eval parse "a: 10";}1b
creates global variable a.
You may fix some scope name, e.g. .l, keep variables there and clear scope before function return, e.g.:
{
eval each (:),'flip (`$".l.",/:string key x;value x);
r: .l.a + .l.b + .l.c;
delete from `.l;
r
}`a`b`c!1 2 3
But getting values directly from input dictionary (like x[`a]) seems easier and clearer approach.
apply helps to simplify invoking other functions, using dictionary values as parameters. This may be what you are looking for. E.g.
{
f: {x+y+z};
f . x`a`b`c
}`a`b`c!1 2 3
I've read that every form in Common Lisp returns something when evaluated. However, recently I've been playing with ASDF API and found a function that returns nothing:
CL-USER> (asdf:clear-output-translations)
; No value
How is this possible and why doesn't it return something like NIL?
Common Lisp allows functions to return from 0 upto MULTIPLE-VALUES-LIMIT values. The constant MULTIPLE-VALUES-LIMIT is 20 or larger.
The function VALUES allows one to return multiple values, including zero values.
Thus a common idiom is to use the form (values) when a function has no useful return value and is just called for side effects. Also this usually causes the Lisp listener (aka REPL) to not print anything as return value, which can be useful for aesthetic reasons.
Note that variables only have a single value and that one can bind only exactly one value to a variable.
Function can return no values by using (values) form.
For example:
(defun foo ()) ;; returns nil
(defun bar () (values)) ;; returns nothing
I've been searching through the documentation and through various questions, but I have not seen a plain answer to this question. What types of values can be passed to Lua functions as arguments?
I believe the answer is:
Plain old data such as Integer or Boolean
Lua Tables
other Lua functions
What I am looking for is a comprehensive list of what all can be passed into a function.
From the Lua reference manual:
All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.
vararg expression as an argument
In addition to passing any value as an argument, there is also an argument that is not a value: ..., the "vararg expression."
It can only be used in the body of a function where it is a formal parameter. When used at the end of a list, it expands the list with all of the actual arguments it represents. When used before the end of a list, it expands the list only with the first actual argument it represents.
local function f(a, b, ...)
print("f", a, b, ..., "end")
end
local function g(x, y, ...)
print( "g", x, y, ...)
end
f()
f(1)
f(1,2)
f(1,2,3)
f(1,2,3,4)
f(1,2,3,4,5)
g()
g(1)
g(1,2)
g(1,2,3)
g(1,2,3,4)
g(1,2,3,4,5)
I'm trying to get the max indexes by row from a matrix. To do this, I'm doing:
[throwaway, indexes] = max(blah, [], 2);
The variable "throwaway," would store values I don't want anymore and would never use, and I don't want to waste memory on it. Is there some way to indicate I don't want anything put into that throwaway variable? Something like undef in Perl, perhaps?
Yes, you can throw away return arguments with ~ for examples if func returns two arguments only v will be available after the following [~, v] = func()
I am starting to learn Lua from Programming in Lua (2nd edition) I didn't understand the following in the book.
network = {
{name ="grauna", IP="210.26.30.34"},
{name ="araial", IP="210.26.30.23"},
}
If we want to sort the table by field name, the author mentions
table.sort(network, function (a,b) return (a.name > b.name) end }
Whats happening here? What does function (a,b) stand for? Is function a key word or something.
If was playing around with it and created a table order
order={x=1,x=22,x=10} // not sure this is legal
and then did
print (table.sort(order,function(a,b) return (a.x > b.x) end))
I did not get any output. Where am I going wrong?
Thanks
It's an anonymous function that takes two arguments and returns true if the first argument is less than the second argument. table.sort() runs this function for each of the elements that need sorting and compares each element with the previous element.
I think (but I am not sure) that order={x=1,x=22,x=10} has the same meaning in Lua as order={x=10}, a table with one key "x" associated with the value 10. Maybe you meant {{x=1},{x=22},{x=10}} to make an "array" of 3 components, each having the key "x".
To answer the second part of your question: Lua is very small, and doesn't provide a way to print a table directly. If you use a table as a list or array, you can do this:
print(unpack(some_table))
unpack({1, 2, 3}) returns 1, 2, 3. A very useful function.
function in lua is a keyword, similar to lambda in Scheme or Common Lisp (& also Python), or fun in Ocaml, to introduce anonymous functions with closed variables, i.e. closures