- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaparray.lua
More file actions
Latest commit
164 lines (142 loc) · 4.23 KB
/
Copy pathmaparray.lua
File metadata and controls
164 lines (142 loc) · 4.23 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
--[[
Map - module to map 1D arrays
--]]
-- {{{ Lambda - Lambda-functions constructor
localLambda= {} do
-- {{{ global scope dependencies
local_assert=assert;
ifnot_assertthenreturnfalseend;
local_getmetatable=_assert(getmetatable);
local_rawequal=_assert( rawequal );
local_select=_assert( select );
local_setmetatable=_assert(setmetatable);
local_table=_assert(table);
local_type=_assert(type);
-- }}} global scope dependencies
-- some different value, where stack to store
local_stack= {};
-- Lambda metatable
local_mt_Lambda= {};
Lambda["mt"] =_mt_Lambda;
local_mt_table= { ["__index"] =_table };
local_new_table=function() return_setmetatable({}, _mt_table) end;
functionLambda.lt(a, b) returna<bend;
functionLambda.index(t, k) returnt[k] end;
Lambda[""] =function(...) return...end;
Lambda["%"] =function(a, b) returna%bend;
Lambda["^"] =function(a, b) returna^bend;
Lambda["-"] =function(a, b)
ifnotbthenreturn-aend;
returna-b;
end;
Lambda["+"] =function(a, b) returna+bend;
Lambda["/"] =function(a, b) returna/bend;
Lambda["*"] =function(a, b) returna*bend;
Lambda[".."] =function(a, b) returna..bend;
Lambda["<"] =function(a, b) returna<bend;
Lambda["<="] =function(a, b) returna<=bend;
Lambda[">"] =function(a, b) returna>bend;
Lambda[">="] =function(a, b) returna>=bend;
Lambda["=="] =function(a, b) returna==bend;
localfunction_is_Lambda(obj)
return_getmetatable(obj) ==_mt_Lambda;
end;
localfunction_unpack_tbl(tbl, cx)
iftbl["n"] <cxthenreturnend;
returntbl[cx], _unpack_tbl(tbl, cx+1);
end;
localfunction_params(self, cx, step, init, ...)
ifcx>#stepthenreturnend;
localval=step[cx];
if_rawequal(val, self) then
val= (...);
elseif_is_Lambda(val) then
val=val(_unpack_tbl(init, 1));
end;
returnval, _params(self, cx+1, step, init, ...);
end;
localfunction_run(self, step, init, ...)
localf=step[1];
if_rawequal(f, nil) thenreturn...end;
if_type(f) =="number" thenreturninit[f] end;
if_rawequal(f, self) thenreturninit[1] end;
returnf(_params(self, 2, step, init, ...));
end;
localfunction_call(self, cx, init, ...)
localstep=self[_stack][cx];
ifnotstepthenreturn...end;
return_call(
self, cx+1, init,
_run(self, step, init, ...)
);
end;
function_make_oper(oper)
returnfunction(a, b)
localobj=a;
ifnot_is_Lambda(obj) thenobj=bend;
obj[_stack]:insert {Lambda[oper], a, b};
returnobj;
end;
end;
_mt_Lambda["__concat"] =_make_oper"..";
_mt_Lambda["__unm"] =function(a)
a[_stack]:insert {Lambda["-"], a};
returna;
end;
_mt_Lambda["__eq"] =_make_oper"==";
_mt_Lambda["__add"] =_make_oper"+";
_mt_Lambda["__sub"] =_make_oper"-";
_mt_Lambda["__mul"] =_make_oper"*";
_mt_Lambda["__div"] =_make_oper"/";
_mt_Lambda["__mod"] =_make_oper"%";
_mt_Lambda["__pow"] =_make_oper"^";
_mt_Lambda["__index"] =function(tbl, k)
tbl[_stack]:insert {Lambda.index, tbl, k};
returntbl;
end;
localfunction_pack_tbl(...)
return {["n"] =_select("#", ...), ...};
end;
_mt_Lambda["__call"] =function(self, ...)
return_call(self, 1, _pack_tbl(...), ...);
end;
functionLambda:apply_params(obj, func, ...)
obj[_stack]:insert {func, ...}
returnobj;
end;
-- {{{ Lambda:new(…) - constructor
-- ... - is a number for parameter placeholder or
-- function with argument list
functionLambda:new(...)
localoperator= (...);
ifoperatorand_type(operator) =="string" then
localoperator=self[operator];
returnfunction(...)
returnself:new(operator, ...);
end;
end;
localp=_setmetatable({[_stack] =_new_table()}, _mt_Lambda);
self:apply_params(p, ...);
returnp;
end;
-- }}} Lambda:new()
end;
-- }}} Lambda
-- map array
localfunctionmap(array, func)
fori=1, #array, 1doarray[i] =func(array[i]) end;
returnarray;
end;
-- map associative array
localfunctionmapa(array, func)
fork, vinpairs(array) doarray[k] =func(v) end;
returnarray;
end;
-- Map wrapper for functions
localfunctionwrap(...) returnLambda:new(...) end;
return {
["Lambda"] =Lambda,
["map"] =map,
["mapa"] =mapa,
["wrap"] =wrap,
};