1 ----------------------------------------------------------------------
2 ----------------------------------------------------------------------
4 -- Base library extension
6 ----------------------------------------------------------------------
7 ----------------------------------------------------------------------
9 if not metalua then rawset(getfenv(), 'metalua', { }) end
10 metalua.version = "v-0.5"
13 rawpairs, rawipairs, rawtype = pairs, ipairs, type
17 assert(type(x)=='table', 'pairs() expects a table')
18 local mt = getmetatable(x)
20 local mtp = mt.__pairs
21 if mtp then return mtp(x) end
27 assert(type(x)=='table', 'ipairs() expects a table')
28 local mt = getmetatable(x)
30 local mti = mt.__ipairs
31 if mti then return mti(x) end
38 local mt = getmetatable(x)
41 if mtt then return mtt end
48 for n in values{...} do if n<a then a=n end end
53 for n in values{...} do if n>a then a=n end end
59 local function g (...)
61 for i=#args, 1, -1 do result = {args[i](unpack(result))} end
62 return unpack (result)
67 function id (...) return ... end
68 function const (k) return function () return k end end
70 function printf(...) return print(string.format(...)) end
72 io.stderr:write(string.format(...).."\n")
76 assert(type(x)=='table', 'ivalues() expects a table')
78 local function iterator ()
79 local r = x[i]; i=i+1; return r
86 assert(type(x)=='table', 'values() expects a table')
87 local function iterator (state)
89 state.content, it = next(state.list, state.content)
92 return iterator, { list = x }
96 assert(type(x)=='table', 'keys() expects a table')
97 local function iterator (state)
98 local it = next(state.list, state.content)
102 return iterator, { list = x }