Simple Lua
Learn Lua basics for NeoVim
20 flashcards · Other · by claudehsu
Which symbol denotes comments? Answer: --
To output string literal 'Hello Lua' we do: print("Hello Lua")
Pay attention to letter case. It's case senstitive.
In Lua, what value represents no value? Answer: nil
How to concatenate string in Lua? local first_name = "Lua" local last_name = "Programming" print(first_name .. " " .. last_name)
Write a simple branch structure in Lua REPL. The structure should contains at least 3 branches.
local magic_number = 10 if magic_number > 0 and magic_number < 5 then print("small number") elseif magic_number < 9 then print("medium number") else print("big number") end Note 'then' is a keyword don't forget 'end'
What operators do we use for equality check?
== to test equality ~= to test inequality, instead of !=
How to invert a boolean value?
use not, instead of the `!` exclamation mark. For example if not true then print("never see this line") else print("this is the line to reveal") end
Write a simple function and pay attention to the syntax.
local function walkDog(dog) print("walk dog: " .. dog) end walkDog("Liyu") Note Don't forget the `end` keyword There is no `do` keyword at the end of the first line As a dynamically typed language, we don't specify the type of the parameter
Define a function that take two numbers and calculates their sum of square
local function sumSqr(a, b) return a * a + b * b end local r = sumSqr(2, 5) print(r) Note the return keyword is used to denote the return value.
Given below code snippet function foo() local x = 10 end foo() print(x) -- what will be printed out? What will be printed out for the print(x) on the last line?
'x' is not defined outside of the function, so we get nil. Note access a non-initialized variable in Lua results in nil instead of raising error. variables belong to scopes. Once we leave that scope, variables in that scope cannot be visited. Think about if we remove the `local` keyword from `local x = 10` in the foo function. What will happen to the print statement? In that case, we are defining a global variable.
Given below code snippet function foo() x = 10 end foo() print(x) What's the execution result?
We get 10 printed out. Without `local`, the variable `x` is defined as a global variable.
Write code snippet for loop structure.
We have two ways to implement loop while loop for loop local count = 0 while count < 5 do print(count) count = count + 1 end for count = 1, 5, 2 do print(count) end Note The `for` condition follows a `start, stop, step` format. stop is included
Describe the compound structure in Lua.
In Lua, there is only one compound structure - Table. We use Table to implement other compound structures such as sequential list and key-value map
How to denote a list in Lua? Can we put items of different types in a list?
List is implemented by table, which is denoted by {}. For example, local dogs = { "Liyu", "Jenis", "Vicky" } for idx = 1, #dogs do print("walk dog: " .. dogs[idx]) end Note Lua is very different from most programming languages on the starting index of the list. It starts from index 1 instead of 0. #dogs stands for the length of the list Items of different types can be put in a list.
How to append an item to a list. For example, given a list local dogs = { "Liyu", "Jenis" } how to append an item called "Vicky" to the list?
table.insert(dogs, "Vicky") print(dogs[3]) Note when insertion index is not provided like above, the item is appended to the tail of the list Lua is more FP style so instead of using the OO style like `dogs.insert(<item>)`, it follows the FP style `table.insert(dogs, <item>)`
Given a list local dogs = { "Liyu", "Jenis" } How to delete the first element "Liyu"?
table.remove(dogs, 1) print(dogs[1])
Define a table with 3 key/value pairs.
local dogs = { ["Mon"] = "Jenis", ["Tue"] = "Liyu", ["Wed"] = "Vicky", } print(dogs["Tue"])
Given a list local dogs = { ["Mon"] = "Jenis", ["Tue"] = "Liyu", ["Wed"] = "Vicky", } How to remove the key/value pair "Wed"/"Vicky" ?
dogs["Wed"] = nil Note By setting the value to nil, we remove the key/value pair.
Suppose we have a file called `greet.lua` and below is its content function Greet() print("Hello from Lua") end How can we utilize the function `Greet` defined in `greet.lua` in another Lua file?
`greet.lua` is considered as a module, and we need to import it require("greet") Greet() print("main is done") Note Above is the content of file `main.lua`, which import `greet` module and uses its function `Greet` The module name is derived from the file base name
Write an example to go thru a table and print all its key/value pairs.
local dogs = { ["Mon"] = "Jenis", ["Tue"] = "Liyu", ["Wed"] = "Vicky", } for key, val in pairs(dogs) do print(key .. " -- " .. val) end Note The `pairs(<table>)` function returns a key-value pair table We use for in loop