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
|
--[[
Functional programming primitives that are occasionally useful
This is not a complete set of primitives, I make them when I need them. Anywhere I use these
heavily (where someone unfamiliar with the functional paradigm might get confused)
I have tried to add comments above or below explaining what it does.
]]
local fn = {}
--Returns a function that "stores" the arguments this function was called with
--For example
--[[
local hiprint = fn.curry(print,"[hi]")
hiprint("world") -- calls print("[hi]","world"), prints "[hi] world"
]]
function fn.curry(func,...)
local args = {...}
return function(...)
local nargs = {}
for k,v in pairs(args) do nargs[k] = v end
for k,v in pairs({...}) do
nargs[#nargs + 1] = v
end
func(table.unpack(nargs))
end
end
--Returns a function that calls all functions starting with the rightmost function
--and calling the "next" function with the returns of the previous function.
--The first function can be called with any arguments like normal
--Anything returned from the last function is returned "out" of this function.
--Example:
--
--local fn = require("fn")
--local printf = fn.compose(print,string.format)
--printf("This is %s shit","funky") --prints "This is some funky shit"
function fn.compose(...)
local nargs = {...}
local lastresult
return function(...)
lastresult = {...}
for n = #nargs, 1, -1 do
lastresult = {nargs[n](table.unpack(lastresult))}
end
return table.unpack(lastresult)
end
end
--Returns a function that applies the given function on a table called with it.
--Example:
--
--local fn = require("fn")
--local add_five = fn.map(function(e) return e + 5 end)
--add_five({1,4,9}) --Returns a table {6,10,14}
function fn.map(func)
return function(tbl)
local ret = {}
for k,v in pairs(tbl) do
ret[k] = func(v)
end
return ret
end
end
--Returns a function that removes anything NOT matching the given function
--Example:
--
--local fn = require("fn")
--local important_skills = fn.filter(function(e) return e > 10 end)
--local usable_skills = imporant_skills({
-- ["wood cutting"] = 5,
-- ["mining"] = 21,
-- ["fighting"] = 12,
-- ["fishing"] = 10,
--})
--for k,v in pairs(usable_skills) print(k,":",v) end --prints:
--mining : 21
--fighting : 12
function fn.filter(func)
return function(tbl)
local ret = {}
for k,v in pairs(tbl) do
if not func(v) then
ret[k] = v
end
end
return ret
end
end
--Get the keys of a table in an array
function fn.keys(tbl)
local ret = {}
for k,v in pairs(tbl) do
table.insert(ret,k)
end
return ret
end
--Get the values of a table in an array
function fn.values(tbl)
local ret = {}
for k,v in pairs(tbl) do
table.insert(ret,v)
end
return ret
end
--Reverses an array
function fn.reverse(tbl)
local ret = {}
local len = #tbl
for i = 1,len do
ret[len - i + 1] = tbl[i]
end
return ret
end
return fn
|