Skip to content

Instantly share code, notes, and snippets.

@spion
Created August 31, 2012 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/3548825 to your computer and use it in GitHub Desktop.
Save spion/3548825 to your computer and use it in GitHub Desktop.
Wrap old-style lua functions to support both old and new styles
local table = require('table');
function wrap(cb, fn)
return function(...)
local arg = {...};
if (type(arg[cb]) == 'function') then
return fn(...);
else
local myargs = {...};
table.insert(myargs, cb, 1);
return function(callback)
myargs[cb] = callback;
return fn(unpack(myargs));
end
end
end
end
local test = wrap(3, function(a, b, cb)
cb(a + b);
end);
print("old style");
test(2, 2, function(res) print(res) end);
print("new style");
test(2, 2)(function(res) print(res) end);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment