Created
March 5, 2012 17:15
-
-
Save Deco/1979549 to your computer and use it in GitHub Desktop.
Ponies!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Fluttershy(f, ...) | |
local decl = {...} | |
return function(...) | |
local arg = {...} | |
for arg_i = 1, math.ceil(#decl/2) do | |
if type(arg[arg_i]) ~= decl[arg_i*2-1] then | |
arg[arg_i] = decl[arg_i*2] | |
end | |
end | |
return f(unpack(arg)) | |
end | |
end | |
x = Fluttershy(print, | |
"string", "not provided!", | |
"number", -9999, | |
"table", {} | |
) | |
print(x(1, 333)) | |
function Assertershy(f, ...) | |
local decl = {...} | |
return function(...) | |
local arg = {...} | |
for arg_i = 1, #arg do | |
assert(type(arg[arg_i]) == decl[arg_i], "no") | |
end | |
return f(unpack(arg)) | |
end | |
end | |
x = Assertershy(print, | |
"string", | |
"number", | |
"table" | |
) | |
print(x(1, 333, {})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment