Currying: Difference between revisions

m
→‎another implementation: use an unique env table variable instead
m (→‎another implementation: fix header markup @ lua)
m (→‎another implementation: use an unique env table variable instead)
Line 1,112:
<lang lua>
local curry do
local call,env = function(fn,...)return fn(...)end
local fmt,cat,rawset,rawget,floor = string.format,table.concat,rawset,rawget,math.floor
local curryHelper = setmetatable({},{
Line 1,122:
s = n<m and fmt('CALL(%s)',r) or fmt('function(...)return ME(%d,%d+select("#",...),%s)end',n,m,s)
local sc = fmt('local %s=... return %s',r,s)
rawset(me,k,(loadstring or load)(sc,'_',nil,{CALL=call,ME=me,select=select}env) )
return rawget(me,k)
end})
env = {CALL=call,ME=curryHelper,select=select}
function curry(...)
local pn,n,fn = select('#',...),...
Anonymous user