Seven More Languages in Seven Weeks (读书笔记):Lua

Lua

  1. Day 1
    1. 字符串拼接:"Hello" .. " world"
    2. REPL:> =(function(num) return 3 * num end)(2)
    3. 函数传参:nil填充
    4. ‘命名’参数:function f(table) ... end; ==> f{a=1,b="2"}
    5. elseif
    6. for i=1,5,2 do ... end
  2. Day 2
    1. 迭代table:for k, v in pairs(t) do result[#result + 1] = k .. ": " .. v end
    2. Metatables
      1. __tostring
      2. __index
      3. __newindex
      4. Prototypes based OO:new = function(self, args...)
        1. setmetatable(obj, self)
        2. self.__index = self
      5. 语法糖:table:method() instead of table.method(self)
    3. Coroutines:create/resume/yield
      1. function fibonacci() ... coroutine.yield(m) ...
      2. generator = coroutine.create(fibonacci)
      3. succeeded, value = coroutine.resume(generator)
  3. Day 3:‘动态语言’vs‘脚本语言’
    1. lua_State* L = luaL_newstate();
      1. luaL_openlibs(L);
      2. luaL_dostring(L, "print('Hello world!')"); //<---
      3. lua_close(L);
    2. lua_pushcfunction(L, midi_send);
      1. ➤lua_setglobal(L, "midi_send");
    3. int midi_send(lua_State* L){
      1. double status = lua_tonumber(L, -3); //第一个参数?栈顶(最后一个参数)是-1
    4. 字符串匹配:local letter, octave, value = string.match(s, "([A-Gs]+)(%d+)(%a+)") //?奇怪的语法

你可能感兴趣的:(编程语言,lua,scripting,协程,metatable)