-
Notifications
You must be signed in to change notification settings - Fork 9
/
less_than.lua
53 lines (43 loc) · 1.25 KB
/
less_than.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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