Lua Cheat Sheet
Intro
Wezterm, Neovim, Yazi等工具都会使用Lua作为配置语言,之前搞Cyberpunk Mod二次修改也是用的Lua。
记录下涉及的Lua的基本语法
Data Types
类型 | 描述 |
---|---|
nil | 表示无效值。条件判断中等同于 false 。 |
boolean | 包含两个literal:true 和 false 。 |
number | 表示double浮点数。Lua 中没有整数类型。 |
string | 字符串。Lua 中的字符串是不可变的 |
table | Lua 中最重要的数据结构,可用来表示数组、字典、集合等。 |
function | 函数 |
userdata | 表示任意 C 数据。主要用于与 C/C++ 代码交互。 |
thread | 表示coroutine,用于实现并发编程。 |
Lua Basic Syntax
Operators
算术操作符 | + | - | * | / | % | ^ |
---|---|---|---|---|---|---|
Description | 加 | 减 | 乘 | 除 | 模 | 幂 |
关系操作符 | == | ~= | > | < | >= | <= |
---|---|---|---|---|---|---|
Description | 等于 | 不等于 | 大于 | 小于 | 大于等于 | 小于等于 |
逻辑操作符 | and | or | not |
---|---|---|---|
Description | 与 | 或 | 非 |
连接操作符 | .. | # |
---|---|---|
Description | 字符串拼接 | 获取变量长度 |
三元运算
Lua没有内置的三元运算符,但可以使用and
和or
来模拟:
1
2
3
4
5
-- 这种写法只在 a 不为 false 或 nil 时完全等同于三元
result = condition and a or b
-- 一个更通用的写法,避免了当 a 为 false 时不等效于三元的情况
result = (condition and {a} or {b})[1]
参考Lua 中的三目运算符 | 菜鸟教程这篇文章理解
使用起来总感觉别扭,不如Python的a if condition else b
直观。
Variable Declaration & Assignment
1
2
3
4
5
6
7
8
-- 全局变量: 默认情况下,所有变量都是全局的
globalVar = 10
-- 局部变量: 使用 local 关键字声明
local localVar = 20
-- 多重赋值
a, b = 1, 2
Flow Control
Condition
1
2
3
4
5
6
7
if condition1 then
-- code
elseif condition2 then
-- code
else
-- code
end
Loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- 1. while loop
while condition do
-- code
end
-- 2. repeat until. Similar to do-while in other languages
repeat
-- code
until condition
-- 3.1 numeric for loop
for i = start, end, step do
-- code
end
arr = {1, 2, 3}
for i = 1, #arr do
-- code
end
-- 3.2. generic for loop
arr = {1, 2, 3}
for index, value in ipairs(arr) do
-- code
end
dict = {a = 1, b = 2}
for key, value in pairs(dict) do
-- code
end
Function
Lua中的函数是一等公民(first-class citizens),可以像其他值一样被操作和传递
Function Definition and Calling
Lua提供了多种定义函数的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-- 标准函数定义
function functionName(parameters)
-- 函数体
return returnValue
end
-- 局部函数定义
local function localFunction(parameters)
-- 函数体
return returnValue
end
-- 匿名函数赋值给变量
local functionVar = function(parameters)
-- 函数体
return returnValue
end
-- 函数调用
result = functionName(arguments)
First-Class Functions
存储在变量中:
1
2
3
4
5local greet = function(name) return "Hello, " .. name end print(greet("World")) -- 输出: Hello, World
作为参数传递给其他函数:
1
2
3
4
5
6
7
8
9local function operate(x, y, operation) return operation(x, y) end local add = function(x, y) return x + y end local multiply = function(x, y) return x * y end print(operate(5, 3, add)) -- 输出: 8 print(operate(5, 3, multiply)) -- 输出: 15
可以作为其他函数的返回值:
1
2
3
4
5
6
7
8
9
10
11local function createMultiplier(factor) return function(n) return n * factor end end local double = createMultiplier(2) local triple = createMultiplier(3) print(double(10)) -- 输出: 20 print(triple(10)) -- 输出: 30
Anonymous Functions
匿名函数是没有名称的函数,通常在需要一个简单、一次性使用的函数时定义。它们经常用于赋值给变量或作为参数传递。类似于 Java 中的 lambda 表达式。
1
2
3
4
5
6
7
-- 赋值给变量
local square = function(x) return x * x end
print(square(5)) -- 输出: 25
-- 作为参数传递 (常见于 table.sort)
local numbers = {5, 1, 9, 3}
table.sort(numbers, function(a, b) return a < b end)
Closures
闭包是计算机科学中的一个重要概念,特别是在函数式编程和面向对象编程中,这里不展开介绍
闭包可以理解成是函数和其相关变量的组合体,通常包含函数本身和函数创建时的环境(变量和作用域)。在[[#First-Class Functions]]例子中的double
就是一个Closure
(闭包),携带了创建时的环境(factor
变量)。
Multiple Return Values
Lua 函数可以返回多个结果,和Python类似。
1
2
3
4
5
6
7
8
9
10
11
12
13
local function getCoordinates()
return 10, 20, "center"
end
local x, y, label = getCoordinates()
-- 如果接收变量少于返回值,多余的返回值会被丢弃
local xOnly = getCoordinates()
print(xOnly) -- 输出: 10
-- 如果接收变量多于返回值,多余的变量会被赋值为 nil
local a, b, c, d = getCoordinates()
print(d) -- 输出: nil
Lua Cheat Sheet