Lua is portable, powerful, embeddable & fast.
git clone https://github.com/LuaDist/lua.git
cloc lua/src
git clone https://github.com/LuaDist/lua.git
cloc lua/src
local name = 'Peter'
local age = 35
local genter = "male"
print(name .. " is a " .. age .. ' years old ' .. gender)
local multilineString = [[
Lorem ipsum
dolor sit amet
]]
-- a single-line Lua comment
--[[
a multi-line comment in Lua
]]
-- Function declaration
local function sum(a,b)
return a + b
end
print(sum(3,2)) -- 5
-- Almost the same as before
local sum = function(a,b)
return a + b
end
print(sum(3,2)) -- 5
local function isEven(n)
if n % 2 == 0 then
return true
end
return false
end
print(isEven(5)) -- false
local function isEven(n)
return n % 2 == 0
end
print(isEven(5)) -- false
local a = {'a', 'b', 'c', 'd'}
print(a[1]) -- a
local length = #a
for i = 1, length do
print(a[i])
end
local a = {'a', 'b', 'c', 'd'}
print(a[1]) -- a
local length = #a
local i = 1
while i < length do
print(a[i])
i = i + 1
end
local a = {'a', 'b', 'c', 'd'}
print(a[1]) -- a
local length = #a
local i = 1
repeat
print(a[i])
i = i + 1
until i > length
local person = {name = 'peter', age = 21}
print(person['name']) -- peter
print(person.name) -- also peter
for key,value in pairs(person) do
print(key .. ' => ' .. value)
end
--[[
name => peter
age => 21
]]
local a = {'a', 'b', 'c', 'd'}
local person = {name = 'peter', age = 21}
print(a[1])
print(person['name'])
print(type(a)) -- table
print(type(person)) -- table
string, number, table, boolean, function, thread, nil
string, math, table, os, io, coroutine, debug
assert load pcall setmetatable
collectgarbage loadfile print tonumber
dofile loadstring rawequal tostring
error module rawget type
rawset xpcall getfenv next
unpack getmetatable require select
ipairs pairs setfenv
// Very little of this stuff:
[9,10,1,2].sort()
> [1,10,2,9]
Key-value data store
require "redis"
redis = Redis.new('localhost', 6379)
redis.set("name", "peter")
name = redis.get("name") # "peter"
redis.set("age", "25")
new_age = redis.incr("age") # 26
Concurrent, but single threaded
counter = redis.get('counter')
redis.incr('counter') if counter.is_number?
counter = redis.get('counter')
########## Multithreading ##########
redis.incr('counter') if counter.is_number?
lua_code =
%{
local counter = redis.call("GET", KEYS[1])
if type(tonumber(counter)) == "number" then
return redis.call("INCR", KEYS[1])
end
}
redis.eval(lua_code, ["counter"])
brew install vim --with-lua
-or-
brew install macvim --with-lua
Startup Speed
Vim script 0 1.498s
Python 0.0166s 0.027s (2000%)
Lua(jit) 0.0002s 0.001s (1152000%)
coronalabs.com/i-want-to-build/games/
coronalabs.com/i-want-to-build/business-apps/
nginx.conf:
http {
server {
location /hello {
content_by_lua_file 'hello.lua'
}
}
}
hello.lua:
local name = ngx.var.arg_name or "Anonymous"
ngx.say("Hello, ", name, "!")
Example:
$ curl http://localhost/hello?name=peter
# Hello, peter!
return function(req, next_middleware)
req.headers['Authorization'] = 'My Secret'
return next_middleware()
end
return function(req, next_middleware)
local res = next_middleware()
res.body = 'Hodor'
return res
end