`
- 浏览:
1295194 次
- 性别:
- 来自:
江苏
-
据某某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" , "/" )
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Programming in Lua Fourth edition 英文版(Lua程序设计)
Programming in Lua(中文版).pdf电子书,高清扫描版
添加书签,方便查阅
Programming in Lua, 3rd.pdfProgramming in Lua, 3rd.pdfProgramming in Lua, 3rd.pdfProgramming in Lua, 3rd.pdf
Programming in Lua的中文高清版
Programming in Lua, 4th Edition
Programming in Lua中文版
《Programming in Lua》的第二版,英文版。介绍Lua 5.1。巨匠之作,不可不读。
Programming In Lua(Lua 脚本语言入门教程 中文版).pdf
Programming In Lua pdf 中文
Programming in Lua 第三版,高清
不多说,学习Lua必备,Roberto Ierusalimschy大作,英文版,mobi格式,供kindle人学习使用。
ProgramminginLua中文版 介绍Lua语法 适用于wow插件开发
LUA编程(programming in lua).pdf
LUA编程(programming in lua) 第一版 文字版
Programming in Lua 最新第三版
programming in lua中文版 pdf格式
Programming in Lua provides a solid base to any programmer who wants to use Lua. It covers all aspects of Lua—from the basics to its API with C. The book is the main source of programming patterns ...
lua中文教程,原名:programming in lua 学习lua不错的教程,分享大家共同进步,一起学习