@@ -3,13 +3,27 @@ if not moon or not moon.inject then
33end
44local util = require (" moonscript.util" )
55local lua = {
6- debug = debug
6+ debug = debug ,
7+ type = type
78}
89dump = util .dump
910p = function (...)
1011 return print (dump (... ))
1112end
12- debug = {
13+ is_object = function (value )
14+ return lua .type (value ) == " table" and value .__class
15+ end
16+ type = function (value )
17+ local base_type = lua .type (value )
18+ if base_type == " table" then
19+ local cls = value .__class
20+ if cls then
21+ return cls
22+ end
23+ end
24+ return base_type
25+ end
26+ debug = setmetatable ({
1327 upvalue = function (fn , k , v )
1428 local upvalues = { }
1529 local i = 1
@@ -31,7 +45,9 @@ debug = {
3145 return lua .debug .setupvalue (fn , upvalues [k ], v )
3246 end
3347 end
34- }
48+ }, {
49+ __index = lua .debug
50+ })
3551run_with_scope = function (fn , scope , ...)
3652 local old_env = getfenv (fn )
3753 local env = setmetatable ({ }, {
@@ -51,7 +67,7 @@ bind_methods = function(obj)
5167 return setmetatable ({ }, {
5268 __index = function (self , name )
5369 local val = obj [name ]
54- if val and type (val ) == " function" then
70+ if val and lua . type (val ) == " function" then
5571 local bound
5672 bound = function (...)
5773 return val (obj , ... )
@@ -94,11 +110,13 @@ extend = function(...)
94110 return tbls [1 ]
95111end
96112copy = function (self )
97- local t = { }
98- for key , val in pairs (self ) do
99- t [key ] = val
100- end
101- return t
113+ return (function ()
114+ local _tbl_0 = { }
115+ for key , val in pairs (self ) do
116+ _tbl_0 [key ] = val
117+ end
118+ return _tbl_0
119+ end )()
102120end
103121mixin = function (self , cls , ...)
104122 local meta = getmetatable (cls )
@@ -131,3 +149,15 @@ mixin_table = function(self, tbl, keys)
131149 end
132150 end
133151end
152+ fold = function (items , fn )
153+ local len = # items
154+ if len > 1 then
155+ local accum = fn (items [1 ], items [2 ])
156+ for i = 3 , len do
157+ accum = fn (acum , items [i ])
158+ end
159+ return accum
160+ else
161+ return items [1 ]
162+ end
163+ end
0 commit comments