create lua.lua

usages of lua
pull/61/head
goer 4 years ago committed by GitHub
parent 1e85fd26ff
commit 637317e7fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,409 @@
--------------------------------------------------------------------------------
-- Lua CHEATSHEET (中文速查表) - by weizhixiangcoder (created on 2020/06/20)
-- Version: 1
-- https://github.com/skywind3000/awesome-cheatsheets
--------------------------------------------------------------------------------
---------------------------------------------------------------------------------
--[[
Lua
2.5C 便宿(C/C++)
使 使宿
lua
Lua 5.3
http://cloudwu.github.io/lua53doc/manual.html
http://www.lua.org/ftp/
--]]
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
--[[
:
local, local
nil false
boolean falsetrue
number
string
function C Lua
table Lua table"关联数组"associative
arrays
thread
userdata C
--]]
---------------------------------------------------------------------------------
print(type(signal)) --nil
signal = true
print(type(signal)) --boolean
signal = 1454
print(type(signal)) --number
signal = "UnionTech"
print(type(signal)) --string
signal = function()
print(type(signal))
end
print(type(signal)) --function
signal = {}
print(type(signal)) --table
signal = coroutine.create(function()
print(type(signal))
end)
print(type(signal)) --coroutine
---------------------------------------------------------------------------------
--[[
if...elseif...else while for
--]]
---------------------------------------------------------------------------------
--if...else
ty_signal = type(signal)
if ty_signal == "coroutine" then
print("signal type is coroutine")
elseif ty_signal == "table" then
print("signal type is table")
else
print("signal type is other")
end
--while
ut_companys = {"beijing company", "shanghai company", "nanjing company", "wuxi company", "guangzhou company", "yunfu company", "wuhan company", "chengdu company", "xian company"}
count = 0
while count <= #ut_companys
do
count = count + 1
print("ut_companys[", count, "] is ", ut_companys[count])
end
--for
for i=#ut_companys, 1, -2 do --以2为步长反向遍历
print("num: ", i, "company: ", ut_companys[i])
end
---------------------------------------------------------------------------------
--[[
table Lua 使便 Map
for
--]]
---------------------------------------------------------------------------------
--table当数组用下标从1开始
for i, c in ipairs(ut_companys) do
print(string.format("1 UnionTech company: %d %s", i, c))
end
table.sort(ut_companys)
for i=#ut_companys, 1, -1 do
print(string.format("2 UnionTech company: %d %s", i, ut_companys[i]))
end
--table当hash map用
ut_cptypes = {}
ut_cptypes["adapter"] = {"beijing company", "wuhan company", "guangzhou company"}
ut_cptypes["developer"] = {"beijing company", "wuhan company", "nanjing company", "chengdu company", "xian company", "guangzhou company"}
ut_cptypes["general"] = {"beijing company"}
for ty, cps in pairs(ut_cptypes) do
for i, cp in ipairs(cps) do
print(string.format("3 UnionTech companys: type:%s identifier:%s company:%s", ty, i, cp))
end
end
---------------------------------------------------------------------------------
--[[
Lua 使
使
--]]
---------------------------------------------------------------------------------
--多重返回值
ut_types = {"adapter", "developer", "general"}
function company_types(cp, cptypes)
local adpt, dvlp, genl = nil, nil, nil
for i, ty in ipairs(ut_types) do
for _, _cp in ipairs(cptypes[ty]) do
if _cp == cp then
if i == 1 then
adpt = true
elseif i == 2 then
dvlp = true
elseif i == 3 then
genl = true
end
break
end
end
end
return adpt, dvlp, genl
end
cp = "wuhan company"
types = {company_types(cp, ut_cptypes)}
for i, ty in ipairs(types) do
if ty then
print(string.format("%s is %s", cp, ut_types[i]))
end
end
--变参
function printf(str, ...)
print(string.format(str, ...))
end
function add_companys(...)
local newcps = {...}
local num = #newcps
for _, cp in ipairs(newcps) do
table.insert(ut_companys, cp)
end
return ut_companys, num
end
_, _ = add_companys("changsha company", "zhengzhou company", "hefei company")
for i=1, #ut_companys do
--print(string.format("4 UnionTech company: %d %s", i, ut_companys[i]))
printf("4 UnionTech company: %d %s", i, ut_companys[i])
end
--闭包
function all_companys(cps)
local companys, n = {}, 0
for _, v in ipairs(cps) do
table.insert(companys, v)
end
return function()
n = n + 1
if n > #companys then
return ""
else
return companys[n]
end
end
end
get_company = all_companys(ut_companys)
while true
do
cp = get_company()
if cp == "" then
break
else
printf("get company: %s", cp)
end
end
---------------------------------------------------------------------------------
--[[
(coroutine)Lua(coroutine)线
西
--]]
---------------------------------------------------------------------------------
function foo (a)
print("foo 函数输出", a)
return coroutine.yield(2 * a) -- 返回 2*a 的值
end
co = coroutine.create(function (a , b)
print("第一次协同程序执行输出", a, b) -- co-body 1 10
local r = foo(a + 1)
print("第二次协同程序执行输出", r)
local r, s = coroutine.yield(a + b, a - b) -- ab的值为第一次调用协同程序时传入
print("第三次协同程序执行输出", r, s)
return b, "结束协同程序" -- b的值为第二次调用协同程序时传入
end)
print("main", coroutine.resume(co, 1, 10)) -- true, 4
print("main", coroutine.resume(co, "r")) -- true 11 -9
print("main", coroutine.resume(co, "x", "y")) -- true 10 end
print("main", coroutine.resume(co, "x", "y")) -- cannot resume dead coroutine
--resume将主协程数据传入次协程 yield将次协程中数据传回主协程
---------------------------------------------------------------------------------
--[[
(Metatable)
Lua 3 :
1.
2. nil
3. __index __index nil nil
__index 123 __index
--]]
---------------------------------------------------------------------------------
father = {
colourofskin = "yellow",
weight = 70,
work = "programming",
otherwork = function()
print "do housework"
end
}
father.__index = father
son = {
weight = 50,
like = "basketball"
}
setmetatable(son, father)
printf("weight:%d like:%s work:%s colourofskin:%s ", son.weight, son.like, son.work, son.colourofskin)
son.otherwork()
---------------------------------------------------------------------------------
--[[
lua lua tablefunction
metatable使lua
--]]
---------------------------------------------------------------------------------
--父类
rect = {
area = 0,
length = 0,
width = 0,
}
function rect:getArea()
if self.area == 0 then
self.area = self.length * self.width
end
return self.area
end
function rect:getLength()
return self.length
end
function rect:new(leng, wid)
self.length = leng
self.width = wid
return self
end
--子类
cuboid = {
volume = 0,
height = 0,
}
function cuboid:getVolume()
if self.volume == 0 then
self.volume = self.height * self:getArea()
end
return self.volume
end
function cuboid:new(_rect, _height)
setmetatable(self, _rect)
_rect.__index = _rect
self.height = _height
return self
end
rect1 = rect:new(5, 10)
print("rect1 rectangle:", rect1:getArea())
cuboid1 = cuboid:new(rect1, 2)
print("cuboid1 volume: ", cuboid1:getVolume())
print("cuboid1 rectangle: ", cuboid1:getArea()) --子类调用父类方法getArea
print("cuboid1 length function: ", cuboid1:getLength()) --子类调用父类方法getLength
print("cuboid1 length variable: ", cuboid1.length) --子类使用父类变量length
--重写子类接口getArea lua中没有重载
function cuboid:getArea()
return 2 * (self.height * self.length + self.height * self.width + self.length * self.width)
end
cuboid2 = cuboid:new(rect1, 2)
print("cuboid2 function: getArea: ", cuboid2:getArea()) --调用子类重写的方法getArea
print("cuboid2 base function: getArea: ", getmetatable(cuboid2):getArea()) --显示调用父类方法getArea
---------------------------------------------------------------------------------
--[[
C: API
----------------------------------------------------------------------
-- 文件名为 module.lua, 定义一个名为 module 的模块
module = {}
module.constant = "这是一个常量"
function module.func1()
io.write("这是一个公有函数!\n")
end
local function func2()
print("这是一个私有函数!")
end
function module.func3()
func2()
end
return module
module
local m = require("module")
print(m.constant)
----------------------------------------------------------------------
LuaC使
Lualoadlib
----------------------------------------------------------------------
--]]
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
--[[
lua 使require
math
table
string
os
io
coroutine
debug
--]]
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
--[[
lua
JavaPython
Lua
--]]
---------------------------------------------------------------------------------
--可在命令行lua lua.lua运行本脚本
Loading…
Cancel
Save