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
73 assert(type(x)=='table', 'ivalues() expects a table')
75 local function iterator ()
76 local r = x[i]; i=i+1; return r
83 assert(type(x)=='table', 'values() expects a table')
84 local function iterator (state)
86 state.content, it = next(state.list, state.content)
89 return iterator, { list = x }
93 assert(type(x)=='table', 'keys() expects a table')
94 local function iterator (state)
95 local it = next(state.list, state.content)
99 return iterator, { list = x }