Lua
最猛的版本还是在【2】里面,15 Min搞定Lua,因为Lua是一种脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。所以会Perl,Python,Shell的话应该很快上手。
变量和控制流
注释:
单行: –
多行:
--[[
--]]
num = 42 – 所有的数字都是double,整数有52bits来存储。
s = ‘first expression’
s2 = ”second expression of string”
muli_line_strings = [[ass
ssss]]
t = nil – 未定义的t;会有垃圾回收
while 循环:
while num < 50 do
num = num + 1
end
IF语句:
if num > 40 then
print('Over 40')
elseif s~= 'hello' then
io.write('Not over 40\n')
else
thisIsGlobal = 5
local line = io.read()
print('Winter is coming,' .. line)
end
Undefined Variable will be nil
foo = anUnknownVariable
aBoolValue = false
-- only nil and false are falsy
if not aBoolValue then
print('twas false')
end
karlSum = 0
for I = 1, 100 do
karlSum = karlSum + i
end
fredSum = 0
for j = 100, 1, -1 do
fredSum = fredSum + j
end
repeat
print('The way of the future')
num = num - 1
until num == 0
函数表示
function fib(n)
if n < 2 then
return 1
end
return fib(n-2) + fib(n-1)
end
-- closures and anonymous functions
function adder(x)
return function(y) return x + y
end
a1 = adder(9)
a2 = adder(36)
print(a1(16)) -- >25
print(a2(64)) --> 100
x, y, z = 1,2,3,4 -- 4 被扔掉
function bar(a,b,c)
print(a,b,c)
return 4,8,15,16,23,42
end
x,y = bar('zaphod') -- print 'zaphod' nil nil
-- x = 4
-- y = 8
-- global function.
function f(x)
return x*x
end
f = function (x) return x*x end
-- local function
local function g(x)
return math.sin(x)
end
local g;
g = function(x)
return math.sin(x)
end
哈希表
t = {key1 = 'value1', key2 = false}
print(t.key1)
t.newkey = {}
t.key2 = nil -- remove key2 from the table.
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
-- use any value as key
print(u[6.28])
a = u['@!#']
b = u[{}] -- b = nil since lookup fails.
function h(x) print(x.key1) end
h{key1 = 'Sonmi~451'} -- Prints 'Sonmi~451'
for key, val in pairs(u) do -- table iteration
print(key, val)
end
print(_G['_G'] == _G) -- Print 'true'
v = {
'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do
print(v[i])
end
f1 = {a = 1, b = 2} -- f1 = 0.5
f2 = {a = 2, b = 3} -- fail: s = f1 + f2
metafraction = {}
function metafraction.__add(f1, f2)
sum = {}
sum.b = f1.b * f2.b
sum.a = f1.a * f2.b + f2.a * f1.b
return sum
end
setmetatable(f1, metafraction)
setmetatable(f2, metafraction)
s = f1 + f2 -- call __add(f1, f2) on f1's metatable
defaultFavs = {animal = 'gru', food = 'donuts'}
myFavs = {food = 'piazza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal
-- __add(a, b) for a + b
-- __sub(a, b) for a - b
-- __mul(a, b) for a * b
-- __div(a, b) for a / b
-- __mod(a, b) for a % b
-- __pow(a, b) for a ^ b
-- __unm(a) for -a
-- __concat(a, b) for a .. b
-- __len(a) for #a
-- __eq(a, b) for a == b
-- __lt(a, b) for a < b
-- __le(a, b) for a <= b
-- __index(a, b) <fn or a table> for a.b
-- __newindex(a, b, c) for a.b = c
-- __call(a, ...) for a(...)
像类一样的table和继承
Dog = {}
function Dog:new()
newObj = {sound = 'woof'}
self.__index = self
return setmetatable(newObj, self)
end
function Dog:makeSound()
print('I say' .. self.sound)
end
mrDog = Dog:new()
mrDog:makeSound() -- 'Print I say woof'
-- mrDog.makeSound(self)
-- 这里的Dog看起来像一个类,实际上是一个table,
继承:继承了所有的变量和函数
LoudDog = Dog:new()
function LoudDog:makeSound()
s = self.sound .. ' '
print(s .. s .. s)
end
seymour = LoudDog:new()
seymour:makeSound() -- 'woof woof woof'
-- 以下的子类和基类一样
function LoudDog:new()
newObj = {}
self.__index = self
return setmetatable(newObj, self)
end
模块化
写一个文件叫做: mod.lua
local M = {}
local