lua-users home
lua-l archive

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

why say that it takes the argument l rather than f or rather than x * x?

On Wed, 7 Nov 2018 at 02:59, Douglas Valenta <> wrote:
Looks like square is a function that is the return value of the call to map. It takes a single table argument, l, and returns the table nl that contains the squares of the values in l. Does that clear it up?


On Tue, Nov 6, 2018, 18:48 Luke <> wrote:
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?