Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

A taste of Lua

Enrique García Cota

@otikik / http://kiki.to

APIStrat, 2014-09

fullscreen

3 main parts:

Intro

Feel

Applications

Part 1: Intro

fullscreen

fullscreen

fullscreen

float-left

Roberto Ierusalimschy

Lua is portable, powerful, embeddable & fast.

Embeddable

General purpose language

~ vs ~

Embedable language

fullscreen

fullscreen

fullscreen

fullscreen

fullscreen

Portable

Portable

⇒ C99

Small

Small

git clone https://github.com/LuaDist/lua.git
cloc lua/src

⇒ ~ 15k (C99)

Small

git clone https://github.com/LuaDist/lua.git
cloc lua/src

⇒ ~ 200KB compiled

Implementations

Lua (5.1, 5.2, 5.3)

LuaJIT

eLua

...

fullscreen

fullscreen

Part 2: Feel

Learn Lua in 15 minutes

http://tylerneylon.com/a/learn-lua/

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
]]

Functions

-- Function declaration
local function sum(a,b)
  return a + b
end

print(sum(3,2)) -- 5

Functions

-- Almost the same as before
local sum = function(a,b)
  return a + b
end

print(sum(3,2)) -- 5

Conditionals

local function isEven(n)
  if n % 2 == 0 then
    return true
  end
  return false
end

print(isEven(5)) -- false

Conditionals

local function isEven(n)
  return n % 2 == 0
end

print(isEven(5)) -- false

Arrays & Numeric Loop

local a = {'a', 'b', 'c', 'd'}
print(a[1]) -- a

local length = #a

for i = 1, length do
  print(a[i])
end

While loop

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

Repeat until loop

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

Hashes / Objects

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
]]

Hash, Array == Table

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

Reuse

Lua tables:

Lua functions:

Types (~7)

string, number, table, boolean, function, thread, nil

Libraries (~7)

string, math, table, os, io, coroutine, debug

Built-in functions (~30)

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

float-left

Libraries

luarocks.org

rocks.moonscript.org

~700 rocks

Lua feels like

A faster, smaller, better javascript


// Very little of this stuff:
[9,10,1,2].sort()
> [1,10,2,9]

Part 3: Applications

fullscreen

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

fullscreen

fullscreen


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"])

centered

brew install vim --with-lua

-or-

brew install macvim --with-lua

fullscreen

fullscreen

                    Startup     Speed

      Vim script    0           1.498s
      Python        0.0166s     0.027s (2000%)
      Lua(jit)      0.0002s     0.001s (1152000%)

centered

Corona SDK

centered

coronalabs.com/i-want-to-build/games/

fullscreen

centered

coronalabs.com/i-want-to-build/business-apps/

centered

www.love2d.org

centered

fullscreen

fullscreen

fullscreen

fullscreen

www.techempower.com/benchmarks

centered

www.techempower.com/benchmarks

centered

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!

fullscreen

fullscreen

fullscreen

fullscreen

fullscreen

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

fullscreen

Conclusion

fullscreen

fullscreen

fullscreen

Questions?

Enrique García Cota

@otikik / http://kiki.to

centered

Thank you!