Download presentation
Presentation is loading. Please wait.
Published byPercival Ray Modified over 8 years ago
1
Fantasy && Smart 吴航
2
Some concepts Differences between Lua & C++ Application
3
DEL + SOL (Sun) + Modula A dynamic scripting language An elegant language A functional language Easy to integrate with C++/Java(glue language)… Efficient Everything is a table Platform-free …
4
Lua Easy to integrate Elegant & Efficient & Platform- free Dynamic scripting (functional) language
5
Global variable Variable assignment a, b = 10, 2*x a=10; b=2*x x, y = y, x -- swap 'x' for 'y' print(b)--> nil b = 10 print(b)--> 10 a dynamic scripting language Lua is a dynamic scripting language
6
nil 、 boolean 、 number ( refers to real instead of integer )、 string 、 userdata 、 function 、 table 、 thread 。 print(type("Hello world"))--> string print(type(10.4*3))--> number print(type(print)) --> function print(type(type))--> function print(type(a))--> nil ('a' is not initialized) a = 10 print(type(a))--> number a = print-- yes, this is valid! a(type(a))--> function
7
Automatically print("10" + 1)--> 11 print("10 + 1")--> 10 + 1 print("-5.3e - 10" * "2")--> -1.06e-09 print("hello" + 1)-- ERROR (cannot convert "hello") print(10.. 20)--> 1020
8
Powerful and smart a={…} Index s = “-”; opnames = {["+"] = "add", ["-"] = "sub", ["*"] = "mul", ["/"] = "div"} print(opnames[s])--> sub Traverse days = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"} revDays = {} for i,v in ipairs(days) do revDays[v] = i end
9
list = nil for line in io.lines() do list = {next=list, value=line} end l = list while l do print(l.value) l = l.next end
10
Variable number of arguments Anonymous function Returning multiple return values of arbitrary type High-order function
11
function g (a, b,...) end CALLPARAMETERS g(3)a=3, b=nil, arg={n=0} g(3, 4)a=3, b=4, arg={n=0} g(3, 4, 5, 8)a=3, b=4, arg={5, 8; n=2}
12
a = {p = print} a.p("Hello World")--> Hello World print = math.sin-- that’s ok! a.p(print(1))--> 0.841470 sin = a.p sin(10, 20)--> 10 20 foo = function (x) return 2*x end
13
s, e = string.find("hello Lua users", "Lua") print(s, e)--> 79 Unpack() f = string.find a = {"hello", "ll"} print(f(unpack(a)))--> 3 4
14
function f1(n) -- n is a local variable local function f2() print(n) – refers to upvalue end --[[ n = n + 10 ]]-- return f2 end g1 = f1(1979) g1() -- > 1979 g2 = f1(500) g2() -- > 500
15
co = coroutine.create(function () print("hi") end) print(co)--> thread: 0x8071d98
16
Three states: suspended 、 running 、 dead print(coroutine.status(co))--> suspended coroutine.resume(co)--> hi print(coroutine.status(co))--> dead
17
co = coroutine.create(function () for i=1,10 do print("co", i) coroutine.yield() end end) coroutine.resume(co)--> co 1 print(coroutine.status(co))--> suspended coroutine.resume(co)--> co 2 coroutine.resume(co)--> co 3 ... coroutine.resume(co)--> co 10 coroutine.resume(co)-- prints nothing
18
function send (x) coroutine.yield(x) end producer = coroutine.create( function () while true do local x = io.read()-- produce new value send(x) end end) function receive () local status, value = coroutine.resume(producer) return value end function consumer () while true do local x = receive()-- receive from producer io.write(x, "\n")-- consume new value end
19
C--API Design Script
20
Wikipedia Programming in Lua http://www.luaer.cn/ http://www.luaer.cn/ http://www.lua.org http://www.lua.org
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.