同步操作将从 fulinux/lua 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/lualocal function languageTest()-- table testlocal names = {"Peter", "Paul", "Mary"}local grades = {Mary=10, Paul=7, Peter=8}table.sort(names, function(n1, n2)return grades[n1] > grades[n2]end)for i=1, #names doprint(names[i])end-- function testlocal function newCounter(name)local i = 0return function()i = i+1return name .. ":" .. iendendlocal c1 = newCounter("c1")local c2 = newCounter("c2")print(c1())print(c1())print(c2())print(c1())print(c2())-- for testlocal function values(t)local i = 0;return function() i=i+1; return t[i] endendfor elm in values(names) doprint(elm)end-- -- for test2-- for k in pairs(names) do-- print(k)-- endendlocal function tableTest()local Set = {}local mt = {}-- create a new set with teh values of the given listSet.new = function(l)local set = {}setmetatable(set, mt)for _, v in ipairs(l) do set[v] = true endreturn set;endSet.union = function(a, b)if getmetatable(a) ~= mt or getmetatable(b) ~= mt thenerror("attempt to 'add' a set with a non-set value", 2);endlocal res = Set.new {}for k in pairs(a) do res[k] = true endfor k in pairs(b) do res[k] = true endreturn resendSet.intersection = function(a, b)local res = Set.new {}for k in pairs(a) dores[k] = b[k]endreturn resendSet.tostring = function(set)local l = {}for e in pairs(set) dol[#l+1] = eendreturn "{" .. table.concat(l, ", ") .. "}"endSet.print = function(s)print(Set.tostring(s))endmt.__add = Set.unionmt.__mul = Set.intersectionmt.__tostring = Set.tostringmt.__le = function(a, b)for k in pairs(a) doif not b[k] then return false endendreturn trueendmt.__lt = function(a, b)return a<=b and not (b<=a)endmt.__eq = function(a, b)return a<=b and b<=aendlocal s1 = Set.new {10, 20, 30, 50}local s2 = Set.new {30, 1}local s3 = s1+s2+s2-- local s3 = s1+s2+s2 + 8Set.print(s3)Set.print((s1+s2)*s1)s1 = Set.new{2, 4}s2 = Set.new{4, 10, 2}print(s1<=s2)print(s1<s2)print(s1>=s2)print(s1>s2)print(s1==s2*s1)s1 = Set.new {10, 4, 5}print(s1)-- mt.__metatable = "not your business"-- print(getmetatable(s1))-- setmetatable(s1, {})local Window = {} -- create a namespace--create teh prototype with default values.Window.prototype = {x=0, y=0, width=100, height=100}Window.mt = {} -- create a metatable--declare the constructor functionfunction Window.new(o)setmetatable(o, Window.mt)return oendWindow.mt.__index = function(table, key)return Window.prototype[key]end-- Window.mt.__index = Window.prototypew = Window.new {x=10, y=20}print(w.x)-- Tables with default valueslocal function setDefault(t, d)local mt = {__index = function() return d end}setmetatable(t, mt)endlocal tab = {x=10, y=20}print(tab.x, tab.z)setDefault(tab, 0)print(tab.x, tab.z)local mt = {__index = function(t) return t.___ end }local function setDefault(t, d)t.___ = dsetmetatable(t, mt)end-- Tracking table accesseslocal t = {} -- original table (created somewhere)-- keep a private access to the original tablelocal _t = t-- create proxyt = {}-- create metatablelocal mt = {__index = function(t, k)print("*access to element " .. tostring(k))return _t[k]end,__newindex = function(t, k, v)print("*update of element " .. tostring(k) .. " to " .. tostring(v))_t[k] = v -- update original tableend}setmetatable(t, mt)t[2] = "hello"print(t[2])-- Read-only tablesfunction readOnly(t)local proxy = {}local mt = {__index = t,__newindex = function(t, k, v)error("attempt to update a read-only table", 2)end}setmetatable(proxy, mt)return proxyenddays = readOnly {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}print(days[1])-- days[2] = "Noday"endlocal function objectTest()local Account = {balance = 0,new = function(self, o)o = o or {} -- create table is user does not provide onesetmetatable(o, self)self.__index = selfreturn oend,withdraw = function(self, v)if v>self.balance then error"insufficient funds" endself.balance = self.balance - vend,deposit = function(self, v)self.balance = self.balance + vend}local a = Account:new {balance = 0}a:deposit(100.00)print(a.balance)local b = Account:new()print(b.balance)b.balance = 123print(b.balance)b:withdraw(100)print(b.balance)print(a.balance)-- inheritancelocal SpecialAccount = Account:new({withdraw = function(self, v)if v - self.balance >= self:getLimit() thenerror"insufficient funds"endself.balance = self.balance - vend,getLimit = function(self)return self.limit or 0end})local s = SpecialAccount:new {limit=1000.00}s:deposit(100.00)s:withdraw(200.00)print("s:", s.balance)endlocal function main()languageTest();tableTest();objectTest();endmain()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。