PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/metalualib/metalua/base.lua

http://github.com/davidm/lua-inspect
Lua | 107 lines | 72 code | 16 blank | 19 comment | 11 complexity | 2c88e61fef7bf3bf9f36435ee5ce0229 MD5 | raw file
  1----------------------------------------------------------------------
  2----------------------------------------------------------------------
  3--
  4-- Base library extension
  5--
  6----------------------------------------------------------------------
  7----------------------------------------------------------------------
  8
  9if not metalua then metalua = {} end --PATCHED.. rawset(getfenv(), 'metalua', { }) end
 10metalua.version             = "v-0.5"
 11
 12if not rawpairs then
 13   rawpairs, rawipairs, rawtype = pairs, ipairs, type
 14end
 15
 16function pairsmt(x) -- PATCHED:LuaInspect [*]
 17   assert(type(x)=='table', 'pairs() expects a table')
 18   local mt = getmetatable(x)
 19   if mt then
 20      local mtp = mt.__pairs
 21      if mtp then return mtp(x) end
 22   end
 23   return rawpairs(x)
 24end
 25
 26function ipairsmt(x) --PATCHED:LuaInspect [*]
 27   assert(type(x)=='table', 'ipairs() expects a table')
 28   local mt = getmetatable(x)
 29   if mt then
 30      local mti = mt.__ipairs
 31      if mti then return mti(x) end
 32   end
 33   return rawipairs(x)
 34end
 35--PATCHED:LuaInspect: [*] For performance, compatibility,
 36--  and debugging reasons, avoid overriding builtins.
 37
 38
 39--[[
 40function type(x)
 41   local mt = getmetatable(x)
 42   if mt then
 43      local mtt = mt.__type
 44      if mtt then return mtt end
 45   end
 46   return rawtype(x)
 47end
 48]]
 49
 50function min (a, ...)
 51   for n in values{...} do if n<a then a=n end end
 52   return a
 53end
 54
 55function max (a, ...)
 56   for n in values{...} do if n>a then a=n end end
 57   return a
 58end
 59
 60function o (...)
 61   local args = {...}
 62   local function g (...)
 63      local result = {...}
 64      for i=#args, 1, -1 do result = {args[i](unpack(result))} end
 65      return unpack (result)
 66   end
 67   return g
 68end
 69
 70function id (...) return ... end
 71function const (k) return function () return k end end
 72
 73function printf(...) return print(string.format(...)) end
 74function eprintf(...) 
 75   io.stderr:write(string.format(...).."\n") 
 76end
 77
 78function ivalues (x)
 79   assert(type(x)=='table', 'ivalues() expects a table')
 80   local i = 1
 81   local function iterator ()
 82      local r = x[i]; i=i+1; return r
 83   end
 84   return iterator
 85end
 86
 87
 88function values (x)
 89   assert(type(x)=='table', 'values() expects a table')
 90   local function iterator (state)
 91      local it
 92      state.content, it = next(state.list, state.content)
 93      return it
 94   end
 95   return iterator, { list = x }
 96end
 97
 98function keys (x)
 99   assert(type(x)=='table', 'keys() expects a table')
100   local function iterator (state)
101      local it = next(state.list, state.content)
102      state.content = it
103      return it
104   end
105   return iterator, { list = x }
106end
107