ソートされたセットの n 番目の要素を 1 つおきに返すために (EVAL 呼び出しを介して) Redis から呼び出される lua スクリプトをまとめようとしています (n 番目はスコアではなく、セット内のランクです)。
構築に使用できる Lua スクリプトのオンライン例はほとんどありません。誰かが私を正しい方向に向けることができますか?
local function copyNOtherElements(table, interval, startpos)
local elemno = 1
local rettab = {}
for k, v in ipairs(table) do
if k >= startpos and (k - startpos) % interval == 0 then
rettab[elemno] = v
elemno = elemno + 1
end
end
return rettab
end
書式設定、電話での入力について申し訳ありません。これは、テーブルが 1 ベースの配列であると仮定しています
将来の読者のために、前の回答に Redis を追加し、N 番目の要素を反復するためのもう少し効率的なコードを追加します。
local function zrange_pick(zset_key, step, start, stop)
-- The next four lines can be removed along with the start/stop params if not needed as in OP Q.
if start == nil than
start = 0
if end == nil than
end = -1
local set_by_score = redis.call('ZRANGE', zset_key, start, end)
local result = {}
for n = 1, #set_by_score, step do
table.insert(result, set_by_score[n])
end
return result
end