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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
--[[
Adds some tools for functional programming. Amends various other namespaces by functions used in a functional context, when they don't make sense on their own.
]]
_libs = _libs or {}
local string, math, table, coroutine = require('string'), require('math'), require('table'), require('coroutine')
functions = {}
boolean = {}
_libs.functions = functions
local functions, boolean = functions, boolean
-- The empty function.
functions.empty = function() end
debug.setmetatable(false, {__index = function(_, k)
return boolean[k] or (_raw and _raw.error or error)('"%s" is not defined for booleans':format(tostring(k)), 2)
end})
for _, t in pairs({functions, boolean, math, string, table}) do
t.fn = function(val)
return function()
return val
end
end
end
-- The identity function.
function functions.identity(...)
return ...
end
-- Returns a function that returns a constant value.
function functions.const(val)
return function()
return val
end
end
-- A function calling function.
function functions.call(fn, ...)
return fn(...)
end
-- A function that executes the provided function if the provided condition is met.
function functions.cond(fn, check)
return function(...)
return check(...) and fn(...) or nil
end
end
--
function functions.args(fn, ...)
local args = {...}
return function(...)
local res = {}
for key, arg in ipairs(args) do
if type(arg) == 'number' then
rawset(res, key, select(arg, ...))
else
rawset(res, key, arg(...))
end
end
return fn(unpack(res))
end
end
-- Returns a function fully applied to the provided arguments.
function functions.prepare(fn, ...)
local args = {...}
return function()
return fn(unpack(args))
end
end
-- Returns a partially applied function, depending on the number of arguments provided.
function functions.apply(fn, ...)
local args = {...}
return function(...)
local res = {}
for key, arg in ipairs(args) do
res[key] = arg
end
local key = #args
for _, arg in ipairs({...}) do
key = key + 1
res[key] = arg
end
return fn(unpack(res))
end
end
-- Returns a partially applied function, with the argument provided at the end.
function functions.endapply(fn, ...)
local args = {...}
return function(...)
local res = {...}
local key = #res
for _, arg in ipairs(args) do
key = key + 1
res[key] = arg
end
return fn(unpack(res))
end
end
-- Returns a function that calls a provided chain of functions in right-to-left order.
function functions.pipe(fn1, fn2)
return function(...)
return fn1(fn2(...))
end
end
-- Returns a closure over the argument el that returns true, if its argument equals el.
function functions.equals(el)
return function(cmp)
return el == cmp
end
end
-- Returns a negation function of a boolean function.
function functions.negate(fn)
return function(...)
return not (true == fn(...))
end
end
-- Returns the ith element of a function.
function functions.select(fn, i)
return function(...)
return select(i, fn(...))
end
end
-- Returns an iterator over the results of a function.
function functions.it(fn, ...)
local res = {fn(...)}
local key = 0
return function()
key = key + 1
return res[key]
end
end
-- Schedules the current function to run delayed by the provided time in seconds and returns the coroutine
function functions.schedule(fn, time, ...)
return coroutine.schedule(fn:prepare(...), time)
end
-- Returns a function that, when called, will execute the underlying function delayed by the provided number of seconds
function functions.delay(fn, time, ...)
local args = {...}
return function()
fn:schedule(time, unpack(args))
end
end
-- Returns a wrapper table representing the provided function with additional functions:
function functions.loop(fn, interval, cond)
if interval <= 0 then
return
end
if type(cond) == 'number' then
cond = function()
local i = 0
local lim = cond
return function()
i = i + 1
return i <= lim
end
end()
end
cond = cond or true:fn()
return coroutine.schedule(function()
while cond() do
fn()
coroutine.sleep(interval)
end
end, 0)
end
--[[
Various built-in wrappers
]]
-- tostring wrapper
function functions.string(fn)
return tostring(fn)
end
-- type wrapper
function functions.type(fn)
return type(fn)
end
-- class wrapper
function functions.class(fn)
return class(fn)
end
local function index(fn, key)
if type(key) == 'number' then
return fn:select(key)
elseif rawget(functions, key) then
return function(...)
return functions[key](...)
end
end
(_raw and _raw.error or error)('"%s" is not defined for functions':format(tostring(key)), 2)
end
local function add(fn, args)
return fn:apply(unpack(args))
end
local function sub(fn, args)
return fn:endapply(unpack(args))
end
-- Assigns a metatable on functions to introduce certain function operators.
-- * fn+{...} partially applies a function to arguments.
-- * fn-{...} partially applies a function to arguments from the end.
-- * fn1..fn2 pipes input from fn2 to fn1.
debug.setmetatable(functions.empty, {
__index = index,
__add = add,
__sub = sub,
__concat = functions.pipe,
__unm = functions.negate,
__class = 'Function'
})
--[[
Logic functions
Mainly used to pass as arguments.
]]
-- Returns true if element is true.
function boolean._true(val)
return val == true
end
-- Returns false if element is false.
function boolean._false(val)
return val == false
end
-- Returns the negation of a value.
function boolean._not(val)
return not val
end
-- Returns true if both values are true.
function boolean._and(val1, val2)
return val1 and val2
end
-- Returns true if either value is true.
function boolean._or(val1, val2)
return val1 or val2
end
-- Returns true if element exists.
function boolean._exists(val)
return val ~= nil
end
-- Returns true if two values are the same.
function boolean._is(val1, val2)
return val1 == val2
end
--[[
Math functions
]]
-- Returns true, if num is even, false otherwise.
function math.even(num)
return num % 2 == 0
end
-- Returns true, if num is odd, false otherwise.
function math.odd(num)
return num % 2 == 1
end
-- Adds two numbers.
function math.add(val1, val2)
return val1 + val2
end
-- Multiplies two numbers.
function math.mult(val1, val2)
return val1 * val2
end
-- Subtracts one number from another.
function math.sub(val1, val2)
return val1 - val2
end
-- Divides one number by another.
function math.div(val1, val2)
return val1 / val2
end
--[[
Table functions
]]
-- Returns an attribute of a table.
function table.get(t, ...)
local res = {}
for i = 1, select('#', ...) do
rawset(res, i, t[select(i, ...)])
end
return unpack(res)
end
-- Returns an attribute of a table without invoking metamethods.
function table.rawget(t, ...)
local res = {}
for i = 1, select('#', ...) do
rawset(res, i, rawget(t, select(i, ...)))
end
return unpack(res)
end
-- Sets an attribute of a table to a specified value.
function table.set(t, ...)
for i = 1, select('#', ...), 2 do
t[select(i, ...)] = select(i + 1, ...)
end
return t
end
-- Sets an attribute of a table to a specified value, without invoking metamethods.
function table.rawset(t, ...)
for i = 1, select('#', ...), 2 do
rawset(t, select(i, ...), select(i + 1, ...))
end
return t
end
-- Looks up the value of a table element in another table
function table.lookup(t, ref, key)
return ref[t[key]]
end
table.it = function()
local it = function(t)
local key
return function()
key = next(t, key)
return t[key], key
end
end
return function(t)
local meta = getmetatable(t)
if not meta then
return it(t)
end
local index = meta.__index
if index == table then
return it(t)
end
local fn = type(index) == 'table' and index.it or index(t, 'it') or it
return (fn == table.it and it or fn)(t)
end
end()
-- Applies function fn to all values of the table and returns the resulting table.
function table.map(t, fn)
local res = {}
for value, key in table.it(t) do
-- Evaluate fn with the element and store it.
res[key] = fn(value)
end
return setmetatable(res, getmetatable(t))
end
-- Applies function fn to all keys of the table, and returns the resulting table.
function table.key_map(t, fn)
local res = {}
for value, key in table.it(t) do
res[fn(key)] = value
end
return setmetatable(res, getmetatable(t))
end
-- Returns a table with all elements from t that satisfy the condition fn, or don't satisfy condition fn, if reverse is set to true. Defaults to false.
function table.filter(t, fn)
if type(fn) ~= 'function' then
fn = functions.equals(fn)
end
local res = {}
for value, key in table.it(t) do
-- Only copy if fn(val) evaluates to true
if fn(value) then
res[key] = value
end
end
return setmetatable(res, getmetatable(t))
end
-- Returns a table with all elements from t whose keys satisfy the condition fn, or don't satisfy condition fn, if reverse is set to true. Defaults to false.
function table.key_filter(t, fn)
if type(fn) ~= 'function' then
fn = functions.equals(fn)
end
local res = {}
for value, key in table.it(t) do
-- Only copy if fn(key) evaluates to true
if fn(key) then
res[key] = value
end
end
return setmetatable(res, getmetatable(t))
end
-- Returns the result of applying the function fn to the first two elements of t, then again on the result and the next element from t, until all elements are accumulated.
-- init is an optional initial value to be used. If provided, init and t[1] will be compared first, otherwise t[1] and t[2].
function table.reduce(t, fn, init)
-- Set the accumulator variable to the init value (which can be nil as well)
local acc = init
for value in table.it(t) do
if init then
acc = fn(acc, value)
else
acc = value
init = true
end
end
return acc
end
-- Return true if any element of t satisfies the condition fn.
function table.any(t, fn)
for value in table.it(t) do
if fn(value) then
return true
end
end
return false
end
-- Return true if all elements of t satisfy the condition fn.
function table.all(t, fn)
for value in table.it(t) do
if not fn(value) then
return false
end
end
return true
end
--[[
String functions.
]]
-- Checks for exact string equality.
function string.eq(str, strcmp)
return str == strcmp
end
-- Checks for case-insensitive string equality.
function string.ieq(str, strcmp)
return str:lower() == strcmp:lower()
end
-- Applies a function to every character of str, concatenates the result.
function string.map(str, fn)
return (str:gsub('.', fn))
end
--[[
Copyright © 2013-2015, Windower
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Windower nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Windower BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]
|