Wednesday, 14 August 2013

How to implement bind() for lua?

How to implement bind() for lua?

I want to implement bind() for Lua, which is widely used in Javascript to
create closures.
Below code demos 1 argument case:
function bind(func, arg1)
return function (...)
return func(arg1, ...)
end
end
local x = { data = 1 }
function x.print(self)
print self.data
end
outputX = bind(x.print, x)
outputX() -- print 1
My question is: how to support any number of binding arguments?

No comments:

Post a Comment