lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Alexander Gladysh wrote:
print(
    extract_keys(
        { a = 1, b = 2, c = 3 },
        "a", "b", "xxx", "c", "yyy"
      )
  )
 --> 1        2        nil         3        nil

An option is using recursive functions:

-- "mapper" is a table:
function map(tab, ...)
  if select("#", ...) > 0 then
    return tab[...], map(tab, select(2,...))
  end
end

-- "mapper" is a function:
function map(func, ...)
  if select("#", ...) > 0 then
    return func(select(1,...)), map(func, select(2,...))
  end
end

--
Shmuel