I'm reading source code written with Torch these days. Torch is a well-known deep learning framework written by Lua.
So I summarize the grammar of it and provide a quick tutorial here.
As we know, Lua is a C-like language. Therefore, it is case-sensitive.
You can use the interrupter of Lua in the command line:
Also, you can run a Lua script with a file in the command line:
The numbers in Lua are all doubles with 64 bits. And you can use the
You can use both double and single quotes for strings. For example:
Also, escape characters still exist in Lua:
The following four lines define the same string:
NULL in C is represented with
nil in Lua. When you try to get values from undefined variables, it will return
nil represent false for a boolean value in Lua. Other values including
0 represent true for a boolean value.
By the way, all variables are global variables unless declared explicitly:
if-else statements, we also present some other grammar of Lua:
- The operator
==tests for equality; the operator
~=is the negation of equality
- The string concatenation operator in Lua is denoted by two dots ('..').
- You can get
The following code calculates the sum of integers from 1 to 100:
The following code calculates the sum of odd numbers from 1 to 100:
The following code calculates the sum of even numbers from 1 to 100:
And here's another example:
The Return Values of Functions
Like Python, you can assign multiple values in one statement, for example:
Please note that the fourth value will be ignored since there're only three variables.
The functions in Lua can return multiple values:
The output in line 2 will be
nil because x is not assigned a value when
f() is invoked. Also, the variable
d in line 6 will also be
Actually, Table is a key-value data structure, as known as Map in other programming languages. We can define a new Table as following:
And you can get or set values as follows:
You can also define a Table in following ways:
And then, you can access values in
anotherTable in a more map-like way:
Now, let's look at arrays.
It is equivalent to:
Please note that the index starts from 1 instead of 0 in Lua.
In addition, you can put different types of variables to an array:
And you can invoke the function in the array with
You can traverse values in an array as follows:
#arr represents the length of the array.
As mentioned above, if you declare a variable without
local, it will be a global variable. All global variables will be stored in a Table named
_G. Suppose there is a global variable named
globalVar, you can access this variable in following ways:
You can traverse values in a table as follows:
Meta Tables and Meta Methods
Metatables and Metamethods are one of the most important grammars in Lua. Metatables allow us to change the behavior of a table. For instance, using metatables, we can define how Lua computes the expression
a + b, where
b are tables. Whenever Lua tries to add two tables, it checks whether either of them has a metatable and whether that metatable has an
__add field. If Lua finds this field, it calls the corresponding value (the so-called metamethod, which should be a function) to compute the sum.
For example, we have two fractions with values of 2/3 and 4/7:
And we are about to add them together. However, we cannot add them together with the operator
+. But we can make it with metamethods:
And now, you can add them together as follows:
__add metamethod corresponds to the operator
+. Other metamethods are listed as following:
There is a function named
__index in metamethods. If you want to make
b as a property of
a, you can do as following:
And here's another example:
When indexing a key in a table, Lua firstly finds it in the table. If the key not exists, it will invoke the
Now, let's talk about object-oriented programming:
Now we can create objects of
The following code gives an example of inherit in Lua:
We can include other lua scripts with the function
require(). Once a script is loaded, it will be executed automatically.
For example, we have a Lua script named
hello.lua with following content:
Once we use
require("hello"), you will get "Hello, World" on your screen. Please note that the script will be executed ONLY once no matter how many times you
require this script.
If you want the script to be executed every time, you can use
dofile function instead. And if you want to load a Lua script without being executed, you can use
You can use it as follows:
Here's a more standard example:
File Name: MyModel.lua
And we can load the model above as follows:
require() function works as follows: