forked from ggcrunchy/solar2d-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_ops.lua
165 lines (140 loc) · 4.02 KB
/
func_ops.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
--- This module defines some basic function primitives.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
-- Modules --
local var_preds = require("var_preds")
-- Imports --
local IsCallable = var_preds.IsCallable
-- Exports --
local M = {}
---@callable func Function to call.
-- @param arg Argument.
-- @return Call results.
function M.Call (func, arg)
return func(arg)
end
--- Multiple-argument variant of @{Call}.
-- @callable func Function to call.
-- @param ... Arguments.
-- @return Call results.
function M.Call_Multi (func, ...)
return func(...)
end
---@param owner Method owner.
-- @param name Method name.
-- @param arg Argument.
-- @return Call results.
function M.CallMethod (owner, name, arg)
return owner[name](owner, arg)
end
--- Multiple-argument variant of @{CallMethod}.
-- @param owner Method owner.
-- @param name Method name.
-- @param ... Arguments.
-- @return Call results.
function M.CallMethod_Multi (owner, name, ...)
return owner[name](owner, ...)
end
--- If _value_ is callable, it is called and its results returned. Otherwise, returns it.
-- @param value Value to call or get.
-- @param arg Call argument.
-- @return Call results or _value_.
function M.CallOrGet (value, arg)
if IsCallable(value) then
return value(arg)
end
return value
end
--- Multiple-argument variant of @{CallOrGet}.
-- @param value Value to call or get.
-- @param ... Call arguments.
-- @return Call results or _value_.
function M.CallOrGet_Multi (value, ...)
if IsCallable(value) then
return value(...)
end
return value
end
---@treturn string **""**.
function M.EmptyString ()
return ""
end
---@treturn boolean **false**.
function M.False ()
return false
end
---@param arg Argument.
-- @return _arg_.
function M.Identity (arg)
return arg
end
--- Returns its arguments, minus the first.
-- @param _ Unused.
-- @param ... Arguments #2 and up.
-- @return Arguments #2 and up.
function M.Identity_AllButFirst (_, ...)
return ...
end
--- Multiple-argument variant of @{Identity}.
-- @param ... Arguments.
-- @return Arguments.
function M.Identity_Multi (...)
return ...
end
--- Builds a function that passes its input to _func_. If it returns a true result,
-- this function returns **false**, and **true** otherwise.
-- @callable func Function to negate, which is passed one argument.
-- @treturn function Negated function.
function M.Negater (func)
return function(arg)
return not func(arg)
end
end
--- Multiple-argument variant of @{Negater}.
-- @callable func Function to negate, which is passed multiple arguments.
-- @treturn function Negated function.
function M.Negater_Multi (func)
return function(...)
return not func(...)
end
end
---@treturn table New empty table.
function M.NewTable ()
return {}
end
--- No operation.
function M.NoOp () end
---@treturn number 1.
function M.One ()
return 1
end
---@treturn boolean **true**.
function M.True ()
return true
end
---@treturn number 0.
function M.Zero ()
return 0
end
-- Export the module.
return M