For example, it's possible to add multiple members to some set in Redis using the sadd
command this way:
sadd myset 38 484 2 92 1
In Lua, I've found I can perform the same operation as follows:
redis.call("SADD", "myset", "38", "484", "2", "92", "1")
But, what happens when the caller doesn't know how many arguments will provide to sadd
?
In JavaScript, there's the Function.apply(...)
function which lets provide arguments in order as an array:
// Source function would look like this: function X(a, b, c) { ... }
X.apply(this, [38, 484, 2]);
How do I achieve the same goal in Lua and Redis?
You can use unpack to create similar functionality to apply:
function apply(f, args) f(unpack(args)) end
function X(a, b, c) print(a, b, c) end
apply(X, {38, 484, 2})
or simply unpack directly:
X(unpack{38, 484, 2})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With