2

我希望能夠映射多個參數的像地圖可變參數功能盧阿

function(a, b) return a+b end 

功能集成到一個表,這樣我可以寫的東西一樣

answer = varmap(function(a, b) return a+b end, {1, 7, 3}, {5, 4, 8} 

但我不舒服LUA可變參數和維基教科書的code samples使用table.getn,當你與#替換它們不起作用,並返回「attempt to preform arithmatic on local 'a' (a nil value)

+0

請不要在沒有接受答案的情況下留下問題。你在一個星期前發佈了這個帖子,並從那以後得到了3個答案。接受的答案可以幫助未來的用戶更快地解決他們的問題,並獎勵幫助最多的人。如果3個答案都沒有幫助,也許更多地強調你的問題。 – warspyking

回答

1

還有一個可能性:

local unpack = table.unpack or unpack 

-------------------------------------------------------------------------------- 
-- Python-like zip() iterator 
-------------------------------------------------------------------------------- 

function zip(...) 
    local arrays, ans = {...}, {} 
    local index = 0 
    return 
    function() 
     index = index + 1 
     for i,t in ipairs(arrays) do 
     if type(t) == 'function' then ans[i] = t() else ans[i] = t[index] end 
     if ans[i] == nil then return end 
     end 
     return ans 
    end 
end 

-------------------------------------------------------------------------------- 

function map(f,...) 
    assert(type(f) == 'function','Function expected for 1st arg') 
    local t = {...} 
    return coroutine.wrap(
     function() 
      for t in zip(unpack(t)) do 
      coroutine.yield(f(unpack(t))) 
      end 
     end) 
end 

-------------------------------------------------------------------------------- 
-- Example use 

for item in map(function(a, b) return a+b end, {1, 7, 3}, {5, 4, 8}) do 
    print(item) 
end 

print() 

for item in map(function(a) return a*2 end, {1, 7, 3}) do 
    print(item) 
end 
0

也許你正在尋找的東西是這樣的:

function varmapn(func, ...) 
    local args, result = { ... }, {} 
    for arg_i = 1, #(args[1]) do 
     local call_args = {} 
     for arg_list = 1, #args do 
     table.insert(call_args, args[arg_list][arg_i]) 
     end 
     table.insert(result, func(table.unpack(call_args))) 
    end 
    return result 
end 

樣品的相互作用:

> answer = varmapn(function (a, b) return a+b end, {1, 7, 3}, {5, 4, 8}) 
> print(answer) 
table: 0x970eb0 
> for i = 1, 3 do print(answer[i]) end 
6 
11 
11 

或者,這裏是一個稍微複雜功能較爲一般。對於參數列表需要陣列,或它需要的表具有任意的鍵:

function mapn(func, ...) 
    local args, call_args = { ... }, {} 
    local result = {} 

    for k in pairs(args[1]) do 
     call_args[k] = {} 
    end 

    for arg_list, v in pairs(args) do 
     for k in pairs(args[1]) do  
     table.insert(call_args[k], v[k]) 
     end 
    end 
    for k, v in pairs(call_args) do 
     result[k] = func(table.unpack(v)) 
    end 
    return result 
end 

樣品的相互作用:

> answer = mapn(function (a, b) return a+b end, {x=1, y=7, z=3}, {x=5, y=4, z=8}) 
> for k,v in pairs(answer) do print(k .. " = " .. v) end 
z = 11 
y = 11 
x = 6 
> answer = mapn(function (a, b) return a+b end, {1, 7, 3}, {5, 4, 8}) 
> for i = 1, 3 do print(answer[i]) end 
6 
11 
11 
0
local function imap(func, ...) -- imap(func, src_table_1, src_table_2, ...) 
    local result = {} 
    local src_tables_arr = {...} 
    if #src_tables_arr == 1 then 
     for k, v in ipairs(src_tables_arr[1]) do 
     result[k] = func(v) 
     end 
    else 
     for k = 1, #src_tables_arr[1] do 
     result[k] = func(
          (table.unpack or unpack) 
          (
           imap(
            function(src_t) return src_t[k] end, 
            src_tables_arr 
           ) 
          ) 
         ) 
     end 
    end 
    return result 
end 
table.imap = imap 

用法:

local arr = table.imap(function (a, b) return a+b end, {1, 7, 3}, {5, 4, 8})