From 048eaac333807429267cd1cfd1bb4a95e1e390c8 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Tue, 8 Jun 2021 14:56:51 +0200 Subject: [PATCH] Functional wrappers for Lua builtins --- func.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/func.lua b/func.lua index d1ce2b4..537f2d1 100644 --- a/func.lua +++ b/func.lua @@ -38,4 +38,76 @@ end --+ Calls func using the provided arguments, deepcopies all arguments function call_by_value(func, ...) return func(unpack(modlib.table.deepcopy{...})) +end + +-- Functional wrappers for Lua's builtin metatable operators (arithmetic, concatenation, length, comparison, indexing, call) + +function add(a, b) + return a + b +end + +function mul(a, b) + return a * b +end + +function div(a, b) + return a / b +end + +function mod(a, b) + return a % b +end + +function pow(a, b) + return a ^ b +end + +function unm(a) + return -a +end + +function concat(a, b) + return a .. b +end + +function len(a) + return #a +end + +function eq(a, b) + return a == b +end + +function lt(a, b) + return a < b +end + +function le(a, b) + return a <= b +end + +function index(object, key) + return object[key] +end + +function newindex(object, key, value) + object[key] = value +end + +function call(object, ...) + object(...) +end + +-- Functional wrappers for logical operators, suffixed with _ to avoid a syntax error + +function not_(a) + return not a +end + +function and_(a, b) + return a and b +end + +function or_(a, b) + return a or b end \ No newline at end of file