2 ms·
> 2. Write me the identity function. (No, really. Stop. Try. Then read on.) In Lua: function id(...) return ... end a, b = id(1, 2) --> a == 1, b == 2
by pygy_ 13y ago
> 2. Write me the identity function. (No, really. Stop. Try. Then read on.)
In Lua:
function id(...) return ... end
a, b = id(1, 2) --> a == 1, b == 2
c, d = id(3) --> c == 3, d == nil
e = id(4, 5, 6) --> e == 4, the rest is discarded.
I use it all the time :-)
The vararg system is built on a stack mechanism, and it is efficient.
Regarding map and other higher level functions, I'd only take the first return value.
If you have tuples it is indeed less of a concern. Better still if you have destructuring assignement, à la Julia.