1 / 21

Fantasy && Smart

Lua. Fantasy && Smart. 吴航. Some concepts Differences between Lua & C++ Application. Outline. 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

anndavis
Download Presentation

Fantasy && Smart

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lua Fantasy && Smart 吴航

  2. Some concepts Differences between Lua & C++ Application Outline

  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 … About Lua

  4. About Lua: Features

  5. Global variable Variable assignment a, b = 10, 2*x <--> a=10; b=2*x x, y = y, x -- swap 'x' for 'y' Lua is a dynamic scripting language print(b) --> nil b = 10 print(b) --> 10

  6. nil、boolean、number(refers to real instead of integer)、string、userdata、function、 table 、thread。 Types of variables 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 Automatic type conversion 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 Table

  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 Example

  10. Variable number of arguments Anonymous function Returning multiple return values of arbitrary type High-order function Function

  11. function g (a, b, ...) end CALL PARAMETERS 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} EXAMPLES

  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 EXAMPLES

  13. s, e = string.find("hello Lua users", "Lua") print(s, e) --> 7 9 Unpack() f = string.find a = {"hello", "ll"} print(f(unpack(a))) --> 3 4 EXAMPLES

  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 Closure

  15. co = coroutine.create(function () print("hi") end) print(co) --> thread: 0x8071d98 Coroutine

  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 end

  19. C--API Design Script Application

  20. Wikipedia Programming in Lua http://www.luaer.cn/ http://www.lua.org Reference

  21. Thanks~ Q&A

More Related