lua-users home
lua-l archive

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

Or perhaps square is not the higher order function: map is its argument not the table?

Or is square not a function, even-though it is defined by something (map) that returns a function, as it is not defined with 'function'?

On Wed, 7 Nov 2018 at 02:29, Luke <> wrote:
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)
  return nl
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?