- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional.lua
More file actions
Latest commit
240 lines (180 loc) · 5.45 KB
/
Copy pathfunctional.lua
File metadata and controls
240 lines (180 loc) · 5.45 KB
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
-- functional by ixtWuko
-- functional programming in Lua
require("extend")
localtinsert=table.insert
localfp= {}
functionfp.Empty()
end
--function fp.Curry(f, num_params)
-- return function(...)
-- local params = table.pack(...)
-- if params.n >= num_params then
-- return f(...)
-- else
-- local fn = function(...)
-- return f(table.unpack(fp.t.Prepend(params, table.pack(...))))
-- end
-- return fp.Curry(fn, num_params - params.n)
-- end
-- end
--end
---@paramffun(...) : any
---@paramnum_paramsinteger
---@returnfun(...) : any
functionfp.Curry(f, num_params)
localcurried= { __f=f, __n=num_params }
curried.__index=curried
curried.__newindex=fp.Empty
curried.__tostring=function(fn) returntostring(fn.__f) end
curried.__call=function(fn, ...)
localparams=table.pack(...)
ifparams.n==0then
returnfn
end
iffn.__pthen
fp.t.Prepend(fn.__p, params)
params.n=params.n+fn.__p.n
end
ifparams.n>=fn.__nthen
returnfn.__f(table.unpack(params))
else
returnfn.__new(params)
end
end
curried.__new=function(params)
localfn= { __p=params }
setmetatable(fn, curried)
returnfn
end
returncurried.__new()
end
functionfp.Compose(...)
localfns=table.pack(...)
returnfunction(...)
localparams=table.pack(...)
fori=fns.n, 1, -1do
params=table.pack(fns[i](table.unpack(params)))
end
returntable.unpack(params)
end
end
localinsert=function(index, value, list)
tinsert(list, index, value)
returnlist
end
localinsertTail=function(value, list)
tinsert(list, value)
returnlist
end
localremove=function(index, list)
returntable.remove(list, index)
end
localremoveTail=function(list)
returntable.remove(list)
end
localprepend=function(source, target)
returntable.prepend(target, source)
end
localappend=function(source, target)
returntable.append(target, source)
end
localfind=function(value, t)
returntable.find(t, value)
end
localfindif=function(condition, t)
returntable.findif(t, condition)
end
localsort=function(comp, list)
table.sort(list, comp)
returnlist
end
localforeach=function(f, t)
returntable.foreach(t, f)
end
localmap=function(f, t)
returntable.map(t, f)
end
localfilter=function(f, t)
returntable.filter(t, f)
end
localreduce=function(f, init, list)
returntable.reduce(list, f, init)
end
localzip=function(f, lhs, rhs)
returntable.zip(lhs, rhs, f)
end
localconcat=function(sep, list)
returntable.concat(list, sep)
end
fp.t= {}
---@typefun(index:integer, value:any, list:table) : table
fp.t.Insert=fp.Curry(insert, 3)
---@typefun(value:any, list:table) : table
fp.t.InsertHead=fp.t.Insert(1)
---@typefun(value:any, list:table) : table
fp.t.InsertTail=fp.Curry(insertTail, 2)
---@typefun(index:integer, list:table) : any
fp.t.Remove=fp.Curry(remove, 2)
---@typefun(list:table) : any
fp.t.RemoveHead=fp.t.Remove(1)
---@typefun(list:table) : any
fp.t.RemoveTail=fp.Curry(removeTail, 1)
---@typefun(source:table, target:table) : table
fp.t.Prepend=fp.Curry(prepend, 2)
---@typefun(source:table, target:table) : table
fp.t.Append=fp.Curry(append, 2)
---@typefun(value:any, t:table)
fp.t.Find=fp.Curry(find, 2)
---@typefun(condition:(fun(key:any, value:any):boolean), t:table)
fp.t.If=fp.Curry(findif, 2)
---@typefun(comp:(fun(a:any, b:any):boolean), list:table) : table
fp.t.Sort=fp.Curry(sort, 2)
---@typefun(f:(fun(v:any):any), t:table)
fp.t.Foreach=fp.Curry(foreach, 2)
---@typefun(f:(fun(v:any):any), t:table) : table
fp.t.Map=fp.Curry(map, 2)
---@typefun(f:(fun(v:any):boolean), t:table) : table
fp.t.Filter=fp.Curry(filter, 2)
---@typefun(f:(fun(a:any, b:any):any), init:any, list:table) : any
fp.t.Reduce=fp.Curry(reduce, 3)
---@typefun(f:(fun(a:any, b:any):any), lhs:table, rhs:table)
fp.t.Zip=fp.Curry(zip, 3)
---@typefun(sep:string, list:table) : string
fp.t.Concat=fp.Curry(concat, 2)
localrep=function(sep, n, s)
returnstring.rep(s, n, sep)
end
localsub=function(i, j, s)
returnstring.sub(s, i, j)
end
localmatch=function(pattern, init, s)
returnstring.match(s, pattern, init)
end
localfind=function(pattern, init, plain, s)
returnstring.find(s, pattern, init, plain)
end
localgmatch=function(pattern, s)
returnstring.gmatch(s, pattern)
end
localgsub=function(pattern, repl, n, s)
returnstring.gsub(s, pattern, repl, n)
end
localsplit=function(delimiter, s)
returnstring.split(s, delimiter)
end
fp.s= {}
---@typefun(sep:string, n:integer, s:string) : string
fp.s.Rep=fp.Curry(rep, 3)
---@typefun(i:integer, j:integer, s:string) : string
fp.s.Sub=fp.Curry(sub, 3)
---@typefun(pattern:string, init:integer, s:string) : any
fp.s.Match=fp.Curry(match, 3)
---@typefun(pattern:string, init:integer, plain:boolean, s:string) : integer, integer, string
fp.s.Find=fp.Curry(find, 4)
---@typefun(pattern:string, s:string) : fun(): string
fp.s.Gmatch=fp.Curry(gmatch, 2)
---@typefun(pattern:string, repl:string, n:integer, s:string) : string, number
fp.s.Gsub=fp.Curry(gsub, 4)
---@typefun(delimiter:string, s:string) : string[]
fp.s.Split=fp.Curry(split, 2)
returnfp