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
来模拟:
LUA
-- 这种写法只在 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
LUA
-- 全局变量: 默认情况下,所有变量都是全局的globalVar = 10-- 局部变量: 使用 local 关键字声明local localVar = 20-- 多重赋值a, b = 1, 2
Flow Control
Condition
LUA
if condition1 then -- codeelseif condition2 then -- codeelse -- codeend
Loop
LUA
-- 1. while loopwhile condition do -- codeend-- 2. repeat until. Similar to do-while in other languagesrepeat -- codeuntil condition-- 3.1 numeric for loopfor i = start, end, step do -- codeendarr = {1, 2, 3}for i = 1, #arr do -- codeend-- 3.2. generic for looparr = {1, 2, 3}for index, value in ipairs(arr) do -- codeenddict = {a = 1, b = 2}for key, value in pairs(dict) do -- codeend
Function
Lua 中的函数是一等公民(first-class citizens),可以像其他值一样被操作和传递
Function Definition and Calling
Lua 提供了多种定义函数的方式:
LUA
-- 标准函数定义function functionName(parameters) -- 函数体 return returnValueend-- 局部函数定义local function localFunction(parameters) -- 函数体 return returnValueend-- 匿名函数赋值给变量local functionVar = function(parameters) -- 函数体 return returnValueend-- 函数调用result = functionName(arguments)
First-Class Functions
存储在变量中:
LUAlocal greet = function(name) return "Hello, " .. nameendprint(greet("World")) -- 输出: Hello, World
作为参数传递给其他函数:
LUAlocal function operate(x, y, operation) return operation(x, y)endlocal add = function(x, y) return x + y endlocal multiply = function(x, y) return x * y endprint(operate(5, 3, add)) -- 输出: 8print(operate(5, 3, multiply)) -- 输出: 15
可以作为其他函数的返回值:
LUAlocal function createMultiplier(factor) return function(n) return n * factor endendlocal double = createMultiplier(2)local triple = createMultiplier(3)print(double(10)) -- 输出: 20print(triple(10)) -- 输出: 30
Anonymous Functions
匿名函数是没有名称的函数,通常在需要一个简单、一次性使用的函数时定义。它们经常用于赋值给变量或作为参数传递。类似于 Java 中的 lambda 表达式。
LUA
-- 赋值给变量local square = function(x) return x * x endprint(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 类似。
LUA
local function getCoordinates() return 10, 20, "center"endlocal x, y, label = getCoordinates()-- 如果接收变量少于返回值,多余的返回值会被丢弃local xOnly = getCoordinates()print(xOnly) -- 输出: 10-- 如果接收变量多于返回值,多余的变量会被赋值为 nillocal a, b, c, d = getCoordinates()print(d) -- 输出: nil
Lua Cheat Sheet