From 4d6e5fdfb051de41a7d872dd42aca6a211d3be7f Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sat, 1 Oct 2022 18:11:44 +0200 Subject: [PATCH] Add less_than function utility module --- init.lua | 1 + less_than.lua | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 less_than.lua diff --git a/init.lua b/init.lua index fd99ba2..3638143 100644 --- a/init.lua +++ b/init.lua @@ -40,6 +40,7 @@ for _, file in pairs{ "schema", "file", "func", + "less_than", "iterator", "math", "table", diff --git a/less_than.lua b/less_than.lua new file mode 100644 index 0000000..81d7dc6 --- /dev/null +++ b/less_than.lua @@ -0,0 +1,53 @@ +-- Comparator utilities for "less than" functions returning whether a < b +local less_than = {} +setfenv(1, less_than) + +default = {} + +function default.less_than(a, b) return a < b end; default.lt = default.less_than +function default.less_or_equal(a, b) return a <= b end; default.leq = default.less_or_equal +function default.greater_than(a, b) return a > b end; default.gt = default.greater_than +function default.greater_or_equal(a, b) return a >= b end; default.geq = default.greater_or_equal + +function less_or_equal(less_than) + return function(a, b) return not less_than(b, a) end +end +leq = less_or_equal + +function greater_or_equal(less_than) + return function(a, b) return not less_than(a, b) end +end +geq = greater_or_equal + +function greater_than(less_than) + return function(a, b) return less_than(b, a) end +end +gt = greater_than + +function equal(less_than) + return function(a, b) + return not (less_than(a, b) or less_than(b, a)) + end +end + +function relation(less_than) + return function(a, b) + if less_than(a, b) then return "<" + elseif less_than(b, a) then return ">" + else return "=" end + end +end + +function by_func(func) + return function(a, b) + return func(a) < func(b) + end +end + +function by_field(key) + return function(a, b) + return a[key] < b[key] + end +end + +return less_than \ No newline at end of file