modlib/func.lua

126 lines
2.6 KiB
Lua
Raw Permalink Normal View History

-- Localize globals
local modlib, unpack, select, setmetatable
= modlib, unpack, select, setmetatable
2021-06-17 19:45:08 +02:00
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
2022-09-30 16:09:16 +02:00
function no_op() end
2020-12-20 15:21:03 +01:00
2022-09-30 16:09:16 +02:00
function identity(...) return ... end
2022-09-09 21:24:20 +02:00
2022-09-30 16:09:16 +02:00
-- TODO switch all of these to proper vargs
2022-09-09 21:24:20 +02:00
2020-03-23 20:20:43 +01:00
function curry(func, ...)
2020-12-20 15:21:03 +01:00
local args = { ... }
return function(...) return func(unpack(args), ...) end
end
function curry_tail(func, ...)
local args = { ... }
2021-04-01 12:48:06 +02:00
return function(...) return func(unpack(modlib.table.concat({...}, args))) end
2020-12-20 15:21:03 +01:00
end
2021-07-03 11:55:14 +02:00
function curry_full(func, ...)
local args = { ... }
return function() return func(unpack(args)) end
end
function args(...)
2020-12-20 15:21:03 +01:00
local args = { ... }
return function(func) return func(unpack(args)) end
end
function value(val) return function() return val end end
function values(...)
local args = { ... }
return function() return unpack(args) end
end
2022-01-18 18:47:38 +01:00
function memoize(func)
return setmetatable({}, {
__index = function(self, key)
local value = func(key)
self[key] = value
return value
end,
__call = function(self, arg)
return self[arg]
end,
__mode = "k"
})
end
2022-09-09 21:24:20 +02:00
function compose(func, other_func)
return function(...)
return func(other_func(...))
end
end
2020-12-20 15:21:03 +01:00
function override_chain(func, override)
return function(...)
func(...)
return override(...)
end
end
2021-03-22 18:17:26 +01:00
--+ Calls func using the provided arguments, deepcopies all arguments
function call_by_value(func, ...)
2022-09-07 11:42:37 +02:00
return func(unpack(modlib.table.deepcopy{...}, 1, select("#", ...)))
2021-06-08 14:56:51 +02:00
end
-- Functional wrappers for Lua's builtin metatable operators (arithmetic, concatenation, length, comparison, indexing, call)
2022-09-30 16:09:16 +02:00
-- TODO (?) add operator table `["+"] = add, ...`
function add(a, b) return a + b end
2021-06-08 14:56:51 +02:00
2022-09-09 21:27:10 +02:00
function sub(a, b) return a - b end
function mul(a, b) return a * b end
2021-06-08 14:56:51 +02:00
function div(a, b) return a / b end
2021-06-08 14:56:51 +02:00
function mod(a, b) return a % b end
2021-06-08 14:56:51 +02:00
function pow(a, b) return a ^ b end
2021-06-08 14:56:51 +02:00
function unm(a) return -a end
2021-06-08 14:56:51 +02:00
function concat(a, b) return a .. b end
2021-06-08 14:56:51 +02:00
function len(a) return #a end
2021-06-08 14:56:51 +02:00
function eq(a, b) return a == b end
2021-06-08 14:56:51 +02:00
2022-09-09 21:27:10 +02:00
function neq(a, b) return a ~= b end
function lt(a, b) return a < b end
2021-06-08 14:56:51 +02:00
2022-09-09 21:27:10 +02:00
function gt(a, b) return a > b end
function le(a, b) return a <= b end
2021-06-08 14:56:51 +02:00
2022-09-09 21:27:10 +02:00
function ge(a, b) return a >= b end
function index(object, key) return object[key] end
2021-06-08 14:56:51 +02:00
function newindex(object, key, value) object[key] = value end
2021-06-08 14:56:51 +02:00
function call(object, ...) object(...) end
2021-06-08 14:56:51 +02:00
-- Functional wrappers for logical operators, suffixed with _ for syntactical convenience
2021-06-08 14:56:51 +02:00
function not_(a) return not a end
_ENV["not"] = not_
2021-06-08 14:56:51 +02:00
function and_(a, b) return a and b end
_ENV["and"] = and_
2021-06-08 14:56:51 +02:00
function or_(a, b) return a or b end
_ENV["or"] = or_
2021-06-17 19:45:08 +02:00
-- Export environment
return _ENV