lua-users home
lua-l archive

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


More from the documentation... thanks for any help!

function map(f)
return function (l)
  local nl = {}
  for i, x in ipairs(l) do
  nl[i] = f(x)
  end
  return nl
  end
end
square = map(function (x) return x * x end)
print(table.unpack(square{ 1, 5, 9 }))

--[[
I believe that, because the definition does not begin with function, square is an anonymous function; it returns a value and is defined in an _expression_. So, I start with the table { 1, 5, 9 }. This is the argument to the square function. I'm guessing that the map function is called by square. But how / why does map get the table, when map only has one parameter, a function, and the table is not assigned to a variable? And how / why does map return a table value, as it seems to want to return a function?

]]