map varargs function lua

时间:2017-02-02 17:19:23

标签: lua functional-programming variadic-functions map-function

我希望能够映射带有多个参数的函数,例如

    function(a, b) return a+b end

到一张桌子上,这样我就可以写出像

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

但我对lua varargs感到不舒服,wikibook上的code samples使用table.getn,当你用#替换它时,它不起作用并返回“{{1} }“

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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})