JD2022-TU1/main/extern/gear4/buildfolder/generation/functools.lua

32 lines
782 B
Lua

-- functools
-- This API implement higher-order functions, inspired by the Python's functools module.
-- Return a function that call f with arg1 as the first argument.
function partial(f, arg1, ...)
assert(f ~= nil)
local function res(...)
return f(arg1, ...)
end
if #arg ~= 0 then
return partial(res, unpack(arg))
else
return res
end
end
-- Return a function that call f with arg2 as the second argument.
function partial2(f, arg2)
assert(f ~= nil)
return function (arg1, ...)
return f(arg1, arg2, ...)
end
end
-- Return a new function which is equivalent to f1(f2(...))
function compose(f1, f2)
assert(f1 ~= nil)
assert(f2 ~= nil)
return function (...)
return f1(f2(...))
end
end