Programming in Lua 读书摘抄

[size=large]
据某某C++的叛徒说"感觉很多地方 lua 都是鄙视 python 的低效 :)"

囧,python的确很慢.是个脚本都喜欢BS它.

lua虽然也很变态,但好歹没有erlang变态

目前还没有看到类似python中dir , help这种好用的函数.还有就是print虽然有语法糖,但是不够甜

不过因为很小,感觉比python清爽不少,很容易就整合到自己的C++程序中间去了.

感觉用给python写扩展是一件很不爽的事情,也难怪那个自称"世界最強絶対無敵電光石火疾風迅雷資料基盤管理器"的Tokyo Cabinet,有 Perl的官方API,有Ruby的官方API,有Java的官方API,有Lua的官方API,就是没有python的官方Api

lua用来写网络也颇有感觉.

以下是 Programming in Lua 读书摘抄,网上有完美的中文pdf,可以找着去下载


1.

lua的不等于 ~=

2.

Lua 语法要求 break 和 return 只能出现在 block 的结尾一句(也就是说:作为 chunk 的最后一句,或者在 end 之前,或者 else前,或者 until前) ,例如:

local i = 1
while a[i] do
if a[i] == v then break end
i = i + 1
end

有时候为了调试或者其他目的需要在 block的中间使用 return或者break,可以显式的使用 do..end 来实现:

function foo ()
return --<< SYNTAX ERROR
-- 'return' is the last statement in the next block
do return end -- OK
... -- statements not reached
end

3.
Lua函数实参和形参的匹配与赋值语句类似,多余部分被忽略,缺少部分用nil补足。
function f(a, b) return a or b end

CALL PARAMETERS

f(3) a=3, b=nil
f(3, 4) a=3, b=4
f(3, 4, 5) a=3, b=4 (5 is discarded)

4.

function foo0 () end -- returns no results
function foo1 () return 'a' end -- returns 1 result
function foo2 () return 'a','b' end -- returns 2 results
第一,当作为表达式调用函数时,有以下几种情况:
1. 当调用作为表达式最后一个参数或者仅有一个参数时,根据变量个数函数尽可能
多地返回多个值,不足补 nil,超出舍去。
2. 其他情况下,函数调用仅返回第一个值(如果没有返回值为 nil)
x,y = foo2() -- x='a', y='b'
x = foo2() -- x='a', 'b' is discarded
x,y,z = 10,foo2() -- x=10, y='a', z='b'

x,y = foo0() -- x=nil, y=nil
x,y = foo1() -- x='a', y=nil
x,y,z = foo2() -- x='a', y='b', z=nil

x,y = foo2(), 20 -- x='a', y=20
x,y = foo0(), 20, 30 -- x='nil', y=20, 30 is discarded

5.

Lib = {}
function Lib.foo (x,y)
return x + y
end
function Lib.goo (x,y)
return x - y
end

6.
模块内部私有函数
local f = function (...)
...
end

7.
for与迭代器

function list_iter (t)
local i = 0
local n = table.getn(t)
return function ()
i = i + 1
if i <= n then return t[i] end
end
end

t = {10, 20, 30}
for element in list_iter(t) do
print(element)
end

8.
无状态的迭代器是指不保留任何状态的迭代器,因此在循环中我们可以利用无状态迭代器避免创建闭包花费额外的代价。

function iter (a, i)
i = i + 1
local v = a[i]
if v then
return i, v
end
end

function ipairs (a)
return iter, a, 0
end

a = {"one", "two", "three"}
for i, v in ipairs(a) do
print(i, v)
end

当 Lua 调用 ipairs(a)开始循环时,他获取三个值:迭代函数 iter、状态常量 a、控制变量初始值 0;
然后 Lua调用 iter(a,0)返回1,a[1] (除非 a[1]=nil);第二次迭代调用 iter(a,1) 返回 2,a[2]……直到第一个非 nil元素。

9.
协同 : 用途 类似生产者 消费者

co = coroutine.create(function ()
print("张沈")
coroutine.yield()
print("鹏")
end)

print(co) --> thread: 0x8071d98
协同有三个状态:
挂起态(suspended)、运行态(running)、停止态(dead)。

当我

创建协同程序成功时,其为挂起态,即此时协同程序并未运行。

我们可用 status 函数检查协同的状态:
print(coroutine.status(co)) --> suspended
函数 coroutine.resume使协同程序由挂起状态变为运行态:
coroutine.resume(co) --> 张沈
coroutine.resume(co) --> 鹏
任务完成,便进入终止态:
print(coroutine.status(co)) --> dead


协同传入参数

co = coroutine.create(function (a,b,c)
print("co", a,b,c)
end)
coroutine.resume(co, 1, 2, 3) --> co 1 2 3

yeild返回参数

co = coroutine.create(function (a,b,c)
print("co", a,b,c)
end)
coroutine.resume(co, 1, 2, 3) --> co 1 2 3

yeild获取参数

co = coroutine.create (function ()
print("co", coroutine.yield())
end)
coroutine.resume(co)
coroutine.resume(co, 4, 5) --> co 4 5

用来写迭代器

function perm (a)
local n = table.getn(a)
return coroutine.wrap(function () permgen(a, n) end)
end

等价于

function perm (a)
local n = table.getn(a)
local co = coroutine.create(function () permgen(a, n) end)
return function () -- iterator
local code, res = coroutine.resume(co)
return res
end
end

10.
阻塞网络
相关文档 http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/tcp.html

require "socket"

function receive (connection)
return connection:receive(2^10)
end

function download (host, file)
local c = assert(socket.connect(host, 80))
local count = 0 -- counts number of bytes read
c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
while true do
local s, status = receive(c)
if status == "closed" then break end
count = count + string.len(s)
end
c:close()
print(file, count)
end

download("www.baidu.com" , "/" )[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值