functions in lua - puc-rioroberto/talks/moscow17.pdf28 the lua-c api functions are constructs found...
TRANSCRIPT
![Page 1: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/1.jpg)
Functions in Lua
Р. Иеруcалимский
![Page 2: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/2.jpg)
2
function values
anonymous functions
lambdas
first-class functions
What does “function” mean?
closures
![Page 3: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/3.jpg)
3
It means several things...
![Page 4: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/4.jpg)
4
Functions are First-Class Values
● Functions are values.– or, there are values that represent functions.
● These values can be stored in variables and data structures.
● They can be passed as arguments to and returned by other functions (higher-order functions).
● They can be called anywhere in a program.
![Page 5: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/5.jpg)
5
Functions can be Nested
● We can define functions inside other functions.– recursively
function foo (x) function p (y) print(y) end p(2*x)end
![Page 6: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/6.jpg)
6
There are Anonymous Functions
● We can write a function without giving a name to it.
● Syntactically, we can write a function as an expression in the language.
add = (function (x,y) return x+y end)
![Page 7: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/7.jpg)
7
Nested Functions have Lexical Scoping
● A function can access local variables from its enclosing functions.
● A function can escape from its enclosing function (e.g., by being returned) and still access those variables.
function makecounter (n) return function (d) n = n + d return n end end
c = makecounter(10)print(c(1)) --> 11print(c(3)) --> 14
![Page 8: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/8.jpg)
8
Properties Somewhat Independent
● C has functions as first-class values, but no nesting.
● Lisp (original) has functions as first-class values and anonymous functions, but no lexical scoping.
● Pascal has lexical scoping, but functions are not first-class values.
● Python 2 and Java have lexical scoping, but only for values.
● Blocks in Ruby and Smalltalk are anonymous with lexical scoping, but they are not first-class values.
![Page 9: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/9.jpg)
9
How Lua uses functions to achieve its goals
![Page 10: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/10.jpg)
10
What are the Goals?
● Portability● Simplicity● Small size● Scripting
![Page 11: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/11.jpg)
11
Portability
● Runs on most platforms we ever heard of:– Posix (Linux, BSD, etc.), OS X, Windows, Android,
iOS, Arduino, Raspberry Pi, Symbian, Nintendo DS, PSP, PS3, IBM z/OS, etc.
– written in ANSI C.
● Runs inside OS kernels.– FreeBSD, Linux
● Written in ANSI C, as a free-standing application.
![Page 12: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/12.jpg)
12
Simplicity
Reference manual with less than 100 pages (proxy for complexity).
(spine)
Documents the language, the libraries, and the C API.
![Page 13: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/13.jpg)
13
Size
Lua 5.3
Lua 1.0
Lua 5.2
Lua 5.1Lua 5.0
Lua4.0
![Page 14: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/14.jpg)
14
Scripting
● Scripting language x dynamic language– scripting emphasizes inter-language communication.
● Program written in two languages.– a scripting language and a system language
● System language implements the hard parts of the application.– algorithms, data structures
– little change
● Scripting glues together the hard parts.– flexible, easy to change
![Page 15: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/15.jpg)
15
Lua and Scripting
● Lua is implemented as a library.● Lua has been designed for scripting.● Good for embedding and extending.● Embedded in C/C++, Java, Fortran, C#, Perl,
Ruby, Python, etc.
![Page 16: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/16.jpg)
16
How Lua uses functions to achieve its goals
![Page 17: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/17.jpg)
17
Modules
● Tables populated with functions
● Several facilities come for free• submodules• local names
local m = require "math"print(m.sqrt(20))local f = m.sqrtprint(f(10))
local math = require "math"print(math.sqrt(10))
![Page 18: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/18.jpg)
18
Modules
● Lexical scoping (for local definitions)● Pros
– needs no new features
– easy to interface with other languages
– flexible
● Cons– not as good as “the real thing” (regarding syntax)
– too dynamic (?)
![Page 19: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/19.jpg)
19
Eval
● Hallmark of dynamic languages.● Lua offers a “compile” function instead.
function eval (code) -- compiles source 'code' and -- executes the result return load(code)()end
function load (code) -- creates an anonymous function -- with the given body return eval("return function () " .. code .. " end")end
![Page 20: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/20.jpg)
20
Load
● Clearly separates compilation from execution.
● load is a pure function.
● It is easier to do eval from load than the reverse.
● Any code always runs inside some function.– we can declare local variables, which naturally work
like static variables for the functions inside the chunk.
– chunks can return values.
![Page 21: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/21.jpg)
21
Exception Handling
● All done through two functions, pcall and error
try { <block/throw>}catch (err) { <exception code>}
local ok, err = pcall(function () <block/error>end)if not ok then <exception code>end
![Page 22: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/22.jpg)
22
Exception Handling
● Anonymous functions with lexical scoping ● Pros
– simple semantics
– no extra syntax
– simple to interface with other languages
● Cons– verbose
– body cannot return/break
– try is not cost-free
![Page 23: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/23.jpg)
23
● Old style:
● New style:
Iterators
local inv = {}table.foreach(t, function (k, v) inf[v] = kend)
for w in allwords(file) do print(w)end
![Page 24: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/24.jpg)
24
function allwords (file) local line = io.read(file) local pos = 1 return function () while line do local w, e = string.match(line, "(%w+)()", pos) if w then pos = e return w else line = io.read(file) pos = 1 end end return nil endend
![Page 25: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/25.jpg)
25
Iterators
● Anonymous functions (for old style), lexical scoping
● Pros– easy to interface with other languages
● Cons– cannot traverse nil
– not so simple as explained
![Page 26: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/26.jpg)
26
function a:foo (x) ...end
a.foo = function (self,x) ...end
a:foo(x) a.foo(a,x)
Objects
● first-class functions + tables ≈ objects● syntactical sugar for methods
• handles self
![Page 27: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/27.jpg)
27
Objects
● Pros– flexible
– easy to interface with other languages
– clear semantics
– needs few new features
● Cons– may need some work to get started (DIY)
– no standard model (DIY)
![Page 28: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/28.jpg)
28
The Lua-C API
● Functions are constructs found in most languages, wich compatible basic semantics.
● Constructions based on functions are easier to translate between different languages.
● Modules, OO programming, and iterators need no extra features in the Lua-C API.– all done with standard mechanisms for tables and
functions.
● Exception handling and load go the opposite way: primitives in the API, exported to Lua.
![Page 29: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/29.jpg)
29
Implementation
● Based on closures.● A closure represents the code of a function plus
the environment where the function was defined.● Lua uses upvalues to represent the
environment, one for each external variable used by the function.
● Zero cost when not used.– variables live on the stack.
![Page 30: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/30.jpg)
30
Closure
Basic data structures
variable in the stack
![Page 31: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/31.jpg)
31
List of open upvalues (for unicity)
![Page 32: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/32.jpg)
32
Closing an upvalue
![Page 33: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/33.jpg)
33
Several Details...
● One-pass compiler.● Safe for space.● Uses flattening for nesting.● List of open upvalues is limited by program
syntax.● A closure may point to upvalues in different
stacks.
![Page 34: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/34.jpg)
34
Final Remarks
● Lua is not only about tables.● Like with tables, Lua itself uses functions for
several important constructs in the language.● In Lua, the use of constructors based on first-
class functions greatly helps to make the C API general.
![Page 35: Functions in Lua - PUC-Rioroberto/talks/moscow17.pdf28 The Lua-C API Functions are constructs found in most languages, wich compatible basic semantics. Constructions based on functions](https://reader033.vdocument.in/reader033/viewer/2022060810/608eb79eb4e18b3cb12e217f/html5/thumbnails/35.jpg)
35