From 0bdf9fe3f31920badaa0718f05db6d954d0a40d0 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 11:37:45 +0100 Subject: [PATCH 01/18] feat: start on lua cheatsheet --- source/_posts/lua.md | 135 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 source/_posts/lua.md diff --git a/source/_posts/lua.md b/source/_posts/lua.md new file mode 100644 index 0000000..9f78172 --- /dev/null +++ b/source/_posts/lua.md @@ -0,0 +1,135 @@ +--- +title: Lua +date: 2025-09-01 10:07:20 +background: bg-[##00007f] +tags: + - script + - interpret +categories: + - Programming +intro: | + The [Lua](https://www.lua.org/) cheat sheet is a one-page reference sheet for the Lua 5.4 programming language. +plugins: + - copyCode + - runCode +--- + +## Getting Started + +### Introduction + +- [Lua](https://www.lua.org/) _(lua.org)_ +- [Lua Document](https://www.lua.org/docs.html) _(lua.org/docs.html)_ +- [Learn X in Y minutes](https://learnxinyminutes.com/docs/lua/) _(learnxinyminutes.com)_ + +### Hello World + +```lua + +print("Hello, World!") -- Hello, World! + +-- You can omit parentheses if the argument is one string or table literal +print "Hello, World!" -- Hello, World! + +print [[multi-line + Hello + World]] + + +``` + +The famous "Hello World" program in Lua + +### Variables + +```lua + +local age = 18 -- local variable +boys, girls = 2, 3 -- global variables + +-- Variables are global by default unless declared with local. + +``` + +Lua is a dynamically typed language and hence only the values will have types not the variables. + +### Comments + +```lua +-- This is a single line comments +``` + +```lua +--[[ + Comments (multi-line) could also be written like this +--]] +``` + +### Data Types {.col-span-2 .row-span-2} + +| Value Type | Description | +| ---------- | ---------------------------------------------- | +| `number` | Represent (double-precision) numbers | +| `string` | Represents text | +| `nil` | Differentiates between values with data or not | +| `boolean` | true or false value | +| `function` | Represents a sub routine | +| `userdata` | Represents arbitrary C data | +| `thread` | Represents independent threads of execution | +| `table` | key-value pair, or array. | + +## Operators + +### Arithmetic + +```lua +-- add + result = 10 + 30 -- => 40 + +-- subtract +result = 40 - 10 -- => 30 + +-- multiply +result = 50 * 5 -- => 250 + +-- divide (float division) +result = 16 / 4 -- => 4.0 + +-- divide (integer division, Lua 5.3+) +result = 16 // 4 -- => 4 + +-- modulo +result = 25 % 2 -- => 1 + +-- power +result = 5 ^ 3 -- => 125 + +-- unary minus +a = 12 +result = -a -- => -12 +``` + +### Relational + +```lua +a = 10 +b = 20 + +-- equals +print(a == b) -- false + +-- not equals +print(a ~= b) -- true + +-- greater than +print(a > b) -- false + +-- less than +print(a < b) -- true + +-- greater than or equals +print(a >= b) -- false + +-- less than or equals +print(a <= b) -- true +``` From 99d67fa4cb3522a9d1896f8ef37faeaa817dec4f Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 11:47:40 +0100 Subject: [PATCH 02/18] feat: add logical operators --- source/_posts/lua.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 9f78172..8b427fd 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -133,3 +133,21 @@ print(a >= b) -- false -- less than or equals print(a <= b) -- true ``` + +### Logical + +```lua +-- and +false and nil --> false +0 and 20 --> 20 +10 and 20 --> 20 + +-- or +true or false --> true +10 or 0 --> 10 +12 or 4 --> 12 + + -- not +not true --> false + +``` From 0b4e2075e4b80d968402c815f4de44090562671c Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 11:59:47 +0100 Subject: [PATCH 03/18] feat: add string data type api --- source/_posts/lua.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 8b427fd..35fdfb0 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -78,6 +78,8 @@ Lua is a dynamically typed language and hence only the values will have types no | `thread` | Represents independent threads of execution | | `table` | key-value pair, or array. | +See: [Data Types](#data-types) + ## Operators ### Arithmetic @@ -151,3 +153,34 @@ true or false --> true not true --> false ``` + +## Data Type APIs + +### Strings + +```lua +s = "Hello" + +-- concatenation +s .. " there" -- => Hello there + +s:upper() -- => HELLO +s:lower() -- => hello +s:len() -- => 5 +s:find("o") -- => 5 +s:reverse() -- => olleH + +-- others +s:match() +s:gmatch() + +s:sub() +s:gsub() + +s:rep() +s:char() +s:dump() +s:byte() +s:format() + +``` From 53a81eebae78f6cbc3908785938d92fef9d5a645 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:15:55 +0100 Subject: [PATCH 04/18] feat: add conditionals and loops --- source/_posts/lua.md | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 35fdfb0..a67b8da 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -154,6 +154,66 @@ not true --> false ``` +## Conditionals + +### if-else {.col-span-3 .row-span-2} + +```lua +num = 15 +if num > 10 then + print("num is greater than 10") +elseif num < 10 then + print("num is smaller than 10") +else + print("num is 10") +end +``` + +## Loops + +### While loop + +```lua +i = 1 +while i <= 5 do + print(i) + i = i + 1 +end +``` + +### For loop + +```lua +-- Numeric for loop (start, end, step) +for i = 1, 5, 1 do + print(i) +end + +-- Generic for loop for tables +t = {10, 20, 30} +for k, v in ipairs(t) do + print(k, v) -- prints 1 10, 2 20, 3 30 +end +``` + +### Repeat-until loop + +```lua +i = 1 +repeat + print(i) + i = i + 1 +until i > 5 +``` + +### Breaking out + +```lua +while x do + if condition then break end +end +``` + ## Data Type APIs ### Strings From af56bbb0d233caa7d40034c0f158a4ea4474d085 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:32:09 +0100 Subject: [PATCH 05/18] feat: add io --- source/_posts/lua.md | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index a67b8da..e3f988f 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -53,18 +53,6 @@ boys, girls = 2, 3 -- global variables Lua is a dynamically typed language and hence only the values will have types not the variables. -### Comments - -```lua --- This is a single line comments -``` - -```lua ---[[ - Comments (multi-line) could also be written like this ---]] -``` - ### Data Types {.col-span-2 .row-span-2} | Value Type | Description | @@ -80,6 +68,30 @@ Lua is a dynamically typed language and hence only the values will have types no See: [Data Types](#data-types) +### IO + +```lua +-- Writes data to the standard output or a file. +io.write("Enter your name: ") +-- Reads input from the user or a file. You can specify formats like "*l" (line), "*n" (number), or "*a" (all). +name = io.read() + +io.write("Enter your age: ") +age = io.read() +``` + +### Comments + +```lua +-- This is a single line comments +``` + +```lua +--[[ + Comments (multi-line) could also be written like this +--]] +``` + ## Operators ### Arithmetic From dea53f82d4c38dfeccb6e5427b44a91beb40d9a7 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:37:29 +0100 Subject: [PATCH 06/18] feat: add Continue --- source/_posts/lua.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index e3f988f..c40c015 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -226,6 +226,19 @@ while x do end ``` +### Continue + +```lua +-- prints even numbers in [|1,10|] +for i=1,10 do + if i % 2 == 1 then + goto continue + end + print(i) + ::continue:: +end +``` + ## Data Type APIs ### Strings From 3e8ffc7ef816b4afcfeb621074fc80ca2184de82 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:51:52 +0100 Subject: [PATCH 07/18] feat: add functions --- source/_posts/lua.md | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index c40c015..553405e 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -47,6 +47,10 @@ The famous "Hello World" program in Lua local age = 18 -- local variable boys, girls = 2, 3 -- global variables +-- Undefined variables return nil. +-- This is not an error: +foo = anUnknownVariable -- Now foo = nil. + -- Variables are global by default unless declared with local. ``` @@ -166,6 +170,8 @@ not true --> false ``` +Only nil and false are falsy; 0 and '' are true! + ## Conditionals ### if-else {.col-span-3 .row-span-2} @@ -239,6 +245,69 @@ for i=1,10 do end ``` +## Functions + +### Creating fns {.col-span-2} + +```lua +function myFunction() + return 1 +end + +function myFunctionWithArgs(a, b) + -- ... +end + +-- function as arg +function operate(a, b, func) + return func(a, b) +end + + + +-- anonymous function +function (parameters) + -- body of the function +end + + +local greet = function(name) + return "Hello, " .. name +end + + +-- Not exported in the module +local function myPrivateFunction() +end + + +-- Splats +function doAction(action, ...) + print("Doing '"..action.."' to", ...) +end + +``` + +### Invoking fns + +```lua +myFunction() + +print(greet("Lua")) -- Output: Hello, Lua + + +-- function as arg +local result = operate(5, 3, function(x, y) + return x + y +end) + +print(result) -- Output: 8 + + +doAction('write', "Shirley", "Abed") +--> Doing 'write' to Shirley Abed +``` + ## Data Type APIs ### Strings From 0f924546486c6208c4687d952a05c625b56f8b89 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:52:52 +0100 Subject: [PATCH 08/18] fix: make break more visible --- source/_posts/lua.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 553405e..d0e11b3 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -228,7 +228,9 @@ until i > 5 ```lua while x do - if condition then break end + if condition then + break + end end ``` From 49c4cfbe72d73da5b398644a200d27e5091beae7 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:54:48 +0100 Subject: [PATCH 09/18] feat: add ternary conditional --- source/_posts/lua.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index d0e11b3..815b935 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -185,6 +185,11 @@ elseif num < 10 then else print("num is 10") end + +-- making a ternary operator +-- This is similar to the a?b:c operator in C/js: +ans = aBoolValue and 'yes' or 'no' --> 'no' + ``` ## Loops From b5e1c92ad10d322a1e7a977cfde7844f2c7d0a73 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:58:58 +0100 Subject: [PATCH 10/18] feat: update functions section col span --- source/_posts/lua.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 815b935..139f197 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -254,7 +254,7 @@ end ## Functions -### Creating fns {.col-span-2} +### Creating fns ```lua function myFunction() @@ -295,7 +295,7 @@ end ``` -### Invoking fns +### Invoking fns {.col-span-2} ```lua myFunction() From c56683d28df52e2719cfcecb6572675ad01c288a Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 12:59:35 +0100 Subject: [PATCH 11/18] feat: add examples of functions without parenthesis --- source/_posts/lua.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 139f197..1efa4ef 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -315,6 +315,21 @@ doAction('write', "Shirley", "Abed") --> Doing 'write' to Shirley Abed ``` +You can omit parentheses if the argument is one string or table literal + +```lua +print "Hello World" --> print("Hello World") + +dofile 'a.lua' --> dofile ('a.lua') + +print [[a multi-line --> print([[a multi-line + message]] message]]) + +f{x=10, y=20} --> f({x=10, y=20}) + +type{} --> type({}) +``` + ## Data Type APIs ### Strings From b85c043af240d0d71915e4c1e4ea568fac4d965e Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 13:02:04 +0100 Subject: [PATCH 12/18] feat: add example for thrown away split initialization var --- source/_posts/lua.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 1efa4ef..1c785cd 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -47,6 +47,9 @@ The famous "Hello World" program in Lua local age = 18 -- local variable boys, girls = 2, 3 -- global variables +-- Now x = 1, y = 2, z = 3, and 4 is thrown away. +x, y, z = 1, 2, 3, 4 + -- Undefined variables return nil. -- This is not an error: foo = anUnknownVariable -- Now foo = nil. From f3f8862402ed76b44e327fa81300b9e65541aebd Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 13:09:11 +0100 Subject: [PATCH 13/18] feat: add Misc & metatables --- source/_posts/lua.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 1c785cd..779c3f0 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -363,3 +363,29 @@ s:byte() s:format() ``` + +## Misc + +### Meta-tables + +A metatable is simply a table with functions in it. + +```lua +mt = {} + +mt.__tostring = function() return "lol" end +mt.__add = function(b) ... end -- a + b +mt.__mul = function(b) ... end -- a * b +mt.__index = function(k) ... end -- Lookups (a[k] or a.k) +mt.__newindex = function(k, v) ... end -- Setters (a[k] = v) +``` + +Metatables allow you to override behavior of another table. + +```lua + +mytable = {} +setmetatable(mytable, mt) + +print(myobject) +``` From 442ce9432e9bdaedfdb9fe6fd24ad2b1fc94f601 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 13:16:52 +0100 Subject: [PATCH 14/18] feat: add classes --- source/_posts/lua.md | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 779c3f0..81d189c 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -389,3 +389,73 @@ setmetatable(mytable, mt) print(myobject) ``` + +### Classes {.col-span-2 .row-span-2} + +Classes aren't built in; there are different ways to make them using tables and metatables. + +```lua + +``` + +Short explanation; what we are trying to do is basically making a table that can hold data and functions + +```lua + +Dog = {} + +function Dog:new() + newObj = {sound = 'woof'} + self.__index = self + return setmetatable(newObj, self) +end + +function Dog:makeSound() + print('I say ' .. self.sound) +end + +mrDog = Dog:new() +mrDog:makeSound() -- 'I say woof' + +``` + +Inheritance + +```lua +LoudDog = Dog:new() + +function LoudDog:makeSound() + s = self.sound .. ' ' + print(s .. s .. s) +end + +seymour = LoudDog:new() +seymour:makeSound() -- 'woof woof woof' +``` + +Another example + +```lua +Account = {} + +function Account:new(balance) + local t = setmetatable({}, { __index = Account }) + + -- Your constructor stuff + t.balance = (balance or 0) + return t +end + +function Account:withdraw(amount) + print("Withdrawing " .. amount .. "...") + self.balance = self.balance - amount + self:report() +end + +function Account:report() + print("Your current balance is: "..self.balance) +end + +a = Account:new(9000) +a:withdraw(200) -- method call +``` From 6f19ada2e4a66001bf50c080c67b19f28fab6f21 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 14:06:28 +0100 Subject: [PATCH 15/18] feat: update Data type APIs --- source/_posts/lua.md | 247 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 214 insertions(+), 33 deletions(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 81d189c..2c1a95f 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -335,61 +335,201 @@ type{} --> type({}) ## Data Type APIs +### Global functions + +Assert + +```lua +local my_table = {} +assert(my_table, "my_table should exist!") -- This will not fail + +local a = nil +-- This will cause a runtime error with the message "a is nil" +assert(a, "a is nil") +``` + +Type + +```lua +local my_var = 10 +print(type(my_var)) -- "number" + +local my_str = "hello" +print(type(my_str)) -- "string" + +local my_func = function() end +print(type(my_func)) -- "function" +``` + +Dofile & Loadfile + +```lua +-- Using dofile +dofile("my_file.lua") -- Executes my_file.lua immediately + +-- Using loadfile +local my_func = loadfile("my_file.lua") +if my_func then + -- my_file.lua is valid, now execute it + my_func() +else + print("Error loading file.") +end +``` + +Pairs + +```lua +local my_table = {10, "hello", 20, name = "Lua"} + +print("Using pairs:") +for key, value in pairs(my_table) do + print(key, value) +end + +print("Using ipairs:") +for key, value in ipairs(my_table) do + print(key, value) +end +``` + +To number + +```lua +local num1 = tonumber("34") +print(num1, type(num1)) -- 34 number + +local num2 = tonumber("34.5") +print(num2, type(num2)) -- 34.5 number + +local num3 = tonumber("abc") +print(num3) -- nil (conversion failed) + +local hex_num = tonumber("8f", 16) +print(hex_num) -- 143 (8 * 16 + 15) +``` + ### Strings ```lua s = "Hello" +``` --- concatenation +Concatenation + +```lua s .. " there" -- => Hello there +``` +Commonly used methods + +```lua s:upper() -- => HELLO s:lower() -- => hello s:len() -- => 5 s:find("o") -- => 5 s:reverse() -- => olleH +``` --- others +Sub + +```lua +local s = "programming" +s:sub(3, 7) -- (extracts substring) => "ogram" +``` + +Gsub + +```lua +s:gsub() --> (substitutes all matches) +``` + +Char + +```lua +s = "ha" +s:rep(3) -- // repeats 3 times -> "hahaha" +local s_char = string.char(72, 101, 108, 108, 111) +print(s_char) -- "Hello" +``` + +Format + +```lua +local name = "Alice" +local age = 30 +local formatted = string.format("My name is %s and I am %d years old.", name, age) +print(formatted) -- "My name is Alice and I am 30 years old." +``` + +Others + +```lua s:match() s:gmatch() - -s:sub() -s:gsub() - -s:rep() -s:char() s:dump() s:byte() -s:format() +``` + +### Table basics + +```lua +-- Array-like table (one-indexed) +local colors = {"red", "green", "blue"} +print(colors[1]) -- "red" + +-- Dictionary-like table +local user = {name = "Jane", age = 25} +print(user.name) -- "Jane" +print(user["age"]) -- 25 + +-- Mixed table +local mixed = {1, "two", key = "value"} +print(mixed[1]) -- 1 +print(mixed.key) -- "value" + +-- Getting the length of an array-like table +print(#colors) -- 3 +``` + +### Tables + +```lua +local my_table = {10, 20} + +-- insert (appends 30 to the end) +table.insert(my_table, 30) + +-- insertion (inserts 2 in position 1) +table.insert(my_table, 1, 2) + +-- remove (remove item in position 3) +table.remove(my_table, 3) + +-- Default numerical sort +local numbers = {5, 2, 8, 1} +table.sort(numbers) -- {1, 2, 5, 8} + +-- Custom sort for descending order +local numbers_desc = {5, 2, 8, 1} +table.sort(numbers_desc, function(a, b) + return a > b +end) -- {8, 5, 2, 1} + +-- concat +local fruit = {"apple", "banana", "cherry"} +local fruit_string = table.concat(fruit, ", ") +print(fruit_string) -- apple, banana, cherry +``` + +### Tables + +```lua ``` ## Misc -### Meta-tables - -A metatable is simply a table with functions in it. - -```lua -mt = {} - -mt.__tostring = function() return "lol" end -mt.__add = function(b) ... end -- a + b -mt.__mul = function(b) ... end -- a * b -mt.__index = function(k) ... end -- Lookups (a[k] or a.k) -mt.__newindex = function(k, v) ... end -- Setters (a[k] = v) -``` - -Metatables allow you to override behavior of another table. - -```lua - -mytable = {} -setmetatable(mytable, mt) - -print(myobject) -``` - ### Classes {.col-span-2 .row-span-2} Classes aren't built in; there are different ways to make them using tables and metatables. @@ -459,3 +599,44 @@ end a = Account:new(9000) a:withdraw(200) -- method call ``` + +### Meta-tables + +A metatable is simply a table with functions in it. + +```lua +mt = {} + +mt.__tostring = function() return "lol" end +mt.__add = function(b) ... end -- a + b +mt.__mul = function(b) ... end -- a * b +mt.__index = function(k) ... end -- Lookups (a[k] or a.k) +mt.__newindex = function(k, v) ... end -- Setters (a[k] = v) +``` + +Metatables allow you to override behavior of another table. + +```lua + +mytable = {} +setmetatable(mytable, mt) + +print(myobject) +``` + +### Files + +```lua +local file = io.open("test.txt", "w") +if file then + file:write("Hello from Lua!") + io.close(file) +end + +local file = io.open("test.txt", "r") +if file then + local content = file:read("*a") -- read all content + print(content) + io.close(file) +end +``` From 47075061270193358984561c12ef2f1e0c3a3087 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 14:22:39 +0100 Subject: [PATCH 16/18] feat: add math fns --- source/_posts/lua.md | 137 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 1 deletion(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index 2c1a95f..bb85e10 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -522,9 +522,144 @@ local fruit_string = table.concat(fruit, ", ") print(fruit_string) -- apple, banana, cherry ``` -### Tables +### Math ```lua +math.abs(-5) -- => 5 +-- Returns the absolute value of x. +``` + +--- + +```lua +math.acos(1) -- => 0 +-- Returns the arc cosine of x in radians. +``` + +--- + +```lua +math.asin(0) -- => 0 +-- Returns the arc sine of x in radians. +``` + +--- + +```lua +math.atan(y, x)` -- => arctangent of y/x +-- Returns the arctangent of `y/x` in radians, using the signs of both arguments to determine the correct quadrant. +``` + +--- + +```lua +math.ceil(x)` -- => smallest integer greater than or equal to x +-- Returns the smallest integer value not less than `x`. +``` + +--- + +```lua +math.cos(x)` -- => cosine of x +-- Returns the cosine of `x` (assumed to be in radians). +``` + +--- + +```lua +math.deg(x)` -- => radians to degrees +-- Returns the angle `x` (in radians) converted to degrees. +``` + +--- + +```lua +math.exp(x)` -- => e^x +-- Returns the value of $e^x$, where $e$ is the base of natural logarithms. +``` + +--- + +```lua +math.floor(x)` -- => largest integer less than or equal to x +-- Returns the largest integer value not greater than `x`. +``` + +--- + +```lua +math.fmod(x, y)` -- => remainder of x divided by y +-- Returns the remainder of `x` divided by `y`, with the same sign as `x`. +``` + +--- + +```lua +math.log(x, base)` -- => logarithm of x +-- Returns the logarithm of `x` in the given `base`. If `base` is not provided, it defaults to the natural logarithm. +``` + +--- + +```lua +math.max(x, ...)` -- => largest argument +-- Returns the largest among its arguments. +``` + +--- + +```lua +math.min(x, ...)` -- => smallest argument +-- Returns the smallest among its arguments. +``` + +--- + +```lua +math.modf(x)` -- => integer and fractional parts +-- Returns two numbers: the integer part of `x` and the fractional part. +``` + +--- + +```lua +math.pow(x, y)` -- => x^y +-- Returns `x` raised to the power of `y`. +``` + +--- + +```lua +math.rad(x)` -- => degrees to radians +-- Returns the angle `x` (in degrees) converted to radians. +``` + +--- + +```lua +math.random(m, n)` -- => random number +-- Returns a pseudo-random number. When called without arguments, returns a float in the range $[0, 1)$. With one argument `n`, returns an integer in the range $[1, n]$. With two arguments `m` and `n`, returns an integer in the range $[m, n]$. +``` + +--- + +```lua +math.sin(x)` -- => sine of x +-- Returns the sine of `x` (assumed to be in radians). +``` + +--- + +```lua +math.sqrt(x)` -- => square root of x +-- Returns the non-negative square root of `x`. +``` + +--- + +```lua +math.tan(x)` -- => tangent of x +-- Returns the tangent of `x` (assumed to be in radians). ``` From 9358967efb374752bc21434b2d15972e2c6c6bec Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 14:25:53 +0100 Subject: [PATCH 17/18] fix: rearrange cards --- source/_posts/lua.md | 106 ++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index bb85e10..dda7581 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -335,7 +335,7 @@ type{} --> type({}) ## Data Type APIs -### Global functions +### Global functions {.col-span-2 .row-span-2} Assert @@ -471,58 +471,9 @@ s:dump() s:byte() ``` -### Table basics +### placeholder -```lua --- Array-like table (one-indexed) -local colors = {"red", "green", "blue"} -print(colors[1]) -- "red" - --- Dictionary-like table -local user = {name = "Jane", age = 25} -print(user.name) -- "Jane" -print(user["age"]) -- 25 - --- Mixed table -local mixed = {1, "two", key = "value"} -print(mixed[1]) -- 1 -print(mixed.key) -- "value" - --- Getting the length of an array-like table -print(#colors) -- 3 -``` - -### Tables - -```lua -local my_table = {10, 20} - --- insert (appends 30 to the end) -table.insert(my_table, 30) - --- insertion (inserts 2 in position 1) -table.insert(my_table, 1, 2) - --- remove (remove item in position 3) -table.remove(my_table, 3) - --- Default numerical sort -local numbers = {5, 2, 8, 1} -table.sort(numbers) -- {1, 2, 5, 8} - --- Custom sort for descending order -local numbers_desc = {5, 2, 8, 1} -table.sort(numbers_desc, function(a, b) - return a > b -end) -- {8, 5, 2, 1} - --- concat -local fruit = {"apple", "banana", "cherry"} -local fruit_string = table.concat(fruit, ", ") -print(fruit_string) -- apple, banana, cherry -``` - -### Math +### Math {.col-span-2 .row-span-2} ```lua math.abs(-5) -- => 5 @@ -663,6 +614,57 @@ math.tan(x)` -- => tangent of x ``` +### Table basics + +```lua +-- Array-like table (one-indexed) +local colors = {"red", "green", "blue"} +print(colors[1]) -- "red" + +-- Dictionary-like table +local user = {name = "Jane", age = 25} +print(user.name) -- "Jane" +print(user["age"]) -- 25 + +-- Mixed table +local mixed = {1, "two", key = "value"} +print(mixed[1]) -- 1 +print(mixed.key) -- "value" + +-- Getting the length of an array-like table +print(#colors) -- 3 +``` + +### Tables + +```lua +local my_table = {10, 20} + +-- insert (appends 30 to the end) +table.insert(my_table, 30) + +-- insertion (inserts 2 in position 1) +table.insert(my_table, 1, 2) + +-- remove (remove item in position 3) +table.remove(my_table, 3) + +-- Default numerical sort +local numbers = {5, 2, 8, 1} +table.sort(numbers) -- {1, 2, 5, 8} + +-- Custom sort for descending order +local numbers_desc = {5, 2, 8, 1} +table.sort(numbers_desc, function(a, b) + return a > b +end) -- {8, 5, 2, 1} + +-- concat +local fruit = {"apple", "banana", "cherry"} +local fruit_string = table.concat(fruit, ", ") +print(fruit_string) -- apple, banana, cherry +``` + ## Misc ### Classes {.col-span-2 .row-span-2} From 44b59c3ed2ebfe805f1cf3510f9eb60c187d6b96 Mon Sep 17 00:00:00 2001 From: Ibrahim Abdulhameed Date: Mon, 1 Sep 2025 14:29:20 +0100 Subject: [PATCH 18/18] fix: data-type-apis href --- source/_posts/lua.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/_posts/lua.md b/source/_posts/lua.md index dda7581..0e1897d 100644 --- a/source/_posts/lua.md +++ b/source/_posts/lua.md @@ -73,7 +73,7 @@ Lua is a dynamically typed language and hence only the values will have types no | `thread` | Represents independent threads of execution | | `table` | key-value pair, or array. | -See: [Data Types](#data-types) +See: [Data Type APIs](#data-type-apis) ### IO