Date: October 1 2022
Summary: My notes on learning the Lua programming language
Keywords: #archive #lua #quick #start #programming
https://github.com/medwatt/Notes/blob/main/Lua/LuaQuickGuide.ipynb
I wanted to learn Lua for no other reason than to program my own workflow in neovim. Also, Lua is cool and Just-in-Time compiled like Julia! Plus, it is used for game development so if ever this research thing doesn't work out, backup plan: become a game developer.
How to write comments within Lua!
-- This is my comment!
--[[
This
is
a
multiline
comment!
--]]
All variables within lua are considered global by default. Moreover, each call or line within a lua script is run as a chunk.
Var = 10
print( "Global var:", Var )
To define a large chunk where variables are local in scope, one can create a local scope using a do-end block
do
local Var = 20
print( "Local Var:", Var )
end
A = A + 1 -- This works
A += 1 -- This does not work
B = 0 -- This works
C = 0 -- This works
B, C = 0, 0 -- This works
B = C = 0 -- This does not work
There are eight basic types within Lua:
nil
- represents absence of data
boolean
number
- integer
and float
both exist as alternative subtypes of number
string
userdata
function
thread
table
print( type("Hello") ) -- string
print( type(10.4 * 3) ) -- number
print( type({1, 2, 3}) ) -- table
print( type(print) ) -- function
print( type(true) ) -- boolean
print( type(nil) ) -- nil
NOTE: Arithmetic operations applied to an object of type
string
will not fail but instead Lua will attempt to convert that object to anumber
type. Vice-versa to this is that if a object of typenumber
is used in astring
operation, Lua will attempt to convert that object to astring
.
A = 100
B = 2
print( A + B ) -- Addition
print( A - B ) -- Subtraction
print( A * B ) -- Multiplication
print( A ^ B ) -- Exponents
print( A / B ) -- Division
print( A // B ) -- Floor
print( A % B ) -- Modulus
print( -A ) -- Negation
A = 20
B = 4
print(A == B) -- false
print(A ~= B) -- true
print(A > B) -- true
print(A < B) -- false
print(A >= B) -- true
print(A <= B) -- false
print(true and 10) -- evaluates to `10`
print(10 and true) -- evaluates `true`
print(false and 10) -- short circuits to `false`
print(false or 10) -- evaluates to `10`
print(nil and 10) -- evaluates to `nil`
print(nil or 10) -- evaluates to `10`
print(false and nil) -- evaluates to `false`
print(false and not(nil)) -- evaluates to `false`
Zelko, Jacob. A Beginner's to Master's Guide in Learning Lua. https://jacobzelko.com/10012022184345-learning-lua. October 1 2022.