Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I provide multiple members to a Redis set using Lua when number of members is unknown until run-time?

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?

like image 829
Matías Fidemraizer Avatar asked Nov 01 '22 09:11

Matías Fidemraizer


1 Answers

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})
like image 129
ryanpattison Avatar answered Nov 09 '22 09:11

ryanpattison