All basics
This commit is contained in:
parent
2f83c4f9a7
commit
336cb76e73
170
README.md
170
README.md
@ -1,3 +1,171 @@
|
|||||||
# cheat-sheet-lua
|
# cheat-sheet-lua
|
||||||
|
|
||||||
Here is the IO-Project cheat sheet to quickly learn the "Lua" programming language
|
Here is the IO-Project cheat sheet to quickly learn the "Lua" programming language
|
||||||
|
|
||||||
|
## Table of contents
|
||||||
|
|
||||||
|
Use this table of contents to travel more easily through this cheat sheet.
|
||||||
|
|
||||||
|
- [cheat-sheet-lua](#cheat-sheet-lua)
|
||||||
|
- [Table of contents](#table-of-contents)
|
||||||
|
- [The basics](#the-basics)
|
||||||
|
- [Code comments](#code-comments)
|
||||||
|
- [Variables and flow control](#variables-and-flow-control)
|
||||||
|
- [Functions](#functions)
|
||||||
|
- [Advanced](#advanced)
|
||||||
|
- [Tables/array](#tablesarray)
|
||||||
|
- [Metatables and metamethods](#metatables-and-metamethods)
|
||||||
|
- [](#)
|
||||||
|
|
||||||
|
## The basics
|
||||||
|
|
||||||
|
### Code comments
|
||||||
|
|
||||||
|
````lua
|
||||||
|
-- Two dashes start a one-line comment.
|
||||||
|
|
||||||
|
--[[
|
||||||
|
by adding two "[" opening and two "]" closing,
|
||||||
|
you define a multi-line commentary. ;-)
|
||||||
|
--]]
|
||||||
|
````
|
||||||
|
|
||||||
|
### Variables and flow control
|
||||||
|
|
||||||
|
Introduction to variables, basic conditions, some loops ([examples available here](support/while.lua)) and the equivalent of the ternary operator.
|
||||||
|
|
||||||
|
````lua
|
||||||
|
num = 42 -- All numbers are doubles.
|
||||||
|
-- Don't freak out, 64-bit doubles have 52 bits for
|
||||||
|
-- storing exact int values; machine precision is
|
||||||
|
-- not a problem for ints that need < 52 bits.
|
||||||
|
|
||||||
|
s = 'walternate' -- Immutable strings like in Python.
|
||||||
|
t = "double-quotes are also fine"
|
||||||
|
u = [[ Double brackets
|
||||||
|
start and end
|
||||||
|
multi-line strings]]
|
||||||
|
t = nil -- Undefines t; Lua has garbage collection.
|
||||||
|
|
||||||
|
-- Blocks are denoted with keywords like do/end:
|
||||||
|
while num < 50 do
|
||||||
|
num = num + 1 -- No ++ or += type operators.
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If clauses:
|
||||||
|
if num > 40 then
|
||||||
|
print('over 40')
|
||||||
|
elseif s ~= 'walternate' then -- ~= is not equals.
|
||||||
|
-- Equality check is ==; ok for strs.
|
||||||
|
io.write('not over 40\n') -- Defaults to stdout.
|
||||||
|
else
|
||||||
|
-- Variables are global by default.
|
||||||
|
thisIsGlobal = 5 -- Camel case is common.
|
||||||
|
|
||||||
|
-- How to make a variable local:
|
||||||
|
local line = io.read() -- Reads next stdin line.
|
||||||
|
|
||||||
|
-- String concatenation uses the .. operator:
|
||||||
|
print('Winter is coming, ' .. line)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Undefined variables return nil.
|
||||||
|
-- This is not an error:
|
||||||
|
foo = anUnknownVariable -- Now foo = nil.
|
||||||
|
|
||||||
|
aBoolValue = false
|
||||||
|
|
||||||
|
-- Only nil and false are false; 0 and '' are true.
|
||||||
|
if not aBoolValue then print('that was false') end
|
||||||
|
|
||||||
|
-- 'or' and 'and' are short-circuited.
|
||||||
|
-- This is similar to the a?b:c operator in C/js:
|
||||||
|
ans = aBoolValue and 'yes' or 'no' --> 'no'
|
||||||
|
|
||||||
|
karlSum = 0
|
||||||
|
for i = 1, 100 do -- The range includes both ends.
|
||||||
|
karlSum = karlSum + i
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Use "100, 1, -1" as the range to count down:
|
||||||
|
fredSum = 0
|
||||||
|
for j = 100, 1, -1 do fredSum = fredSum + j end
|
||||||
|
|
||||||
|
-- In general, the range is begin, end[, step].
|
||||||
|
|
||||||
|
-- Another loop construct:
|
||||||
|
repeat
|
||||||
|
print('the way of the future')
|
||||||
|
num = num - 1
|
||||||
|
until num == 0
|
||||||
|
````
|
||||||
|
|
||||||
|
### Functions
|
||||||
|
|
||||||
|
Abordons la définition de fonctions plus complètes, la récurcivité, les closures. Découverte de l'asignation d'une suite de valeur a une suite de variable.
|
||||||
|
|
||||||
|
````lua
|
||||||
|
-- The famous Fibonacci sequence.
|
||||||
|
function fib(n)
|
||||||
|
if n < 2 then return 1 end
|
||||||
|
return fib(n - 2) + fib(n - 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Closures and anonymous functions are ok:
|
||||||
|
function adder(x)
|
||||||
|
-- The returned function is created when adder is
|
||||||
|
-- called, and remembers the value of x:
|
||||||
|
return function (y) return x + y end
|
||||||
|
end
|
||||||
|
a1 = adder(9)
|
||||||
|
a2 = adder(36)
|
||||||
|
print(a1(16)) --> 25
|
||||||
|
print(a2(64)) --> 100
|
||||||
|
|
||||||
|
-- Returns, func calls, and assignments all work
|
||||||
|
-- with lists that may be mismatched in length.
|
||||||
|
-- Unmatched receivers are nil;
|
||||||
|
-- unmatched senders are discarded.
|
||||||
|
|
||||||
|
x, y, z = 1, 2, 3, 4
|
||||||
|
-- Now x = 1, y = 2, z = 3, and 4 is thrown away.
|
||||||
|
|
||||||
|
function bar(a, b, c)
|
||||||
|
print(a, b, c)
|
||||||
|
return 4, 8, 15, 16, 23, 42
|
||||||
|
end
|
||||||
|
|
||||||
|
x, y = bar('zaphod') --> prints "zaphod nil nil"
|
||||||
|
-- Now x = 4, y = 8, values 15..42 are discarded.
|
||||||
|
|
||||||
|
-- Functions are first-class, may be local/global.
|
||||||
|
-- (global) These are the same:
|
||||||
|
function f(x) return x * x end
|
||||||
|
f = function (x) return x * x end -- same of javascript definition
|
||||||
|
|
||||||
|
-- (local) And so are these:
|
||||||
|
local function g(x) return math.sin(x) end
|
||||||
|
local g; g = function (x) return math.sin(x) end
|
||||||
|
-- the 'local g' decl makes g-self-references ok.
|
||||||
|
|
||||||
|
-- Trig funcs work in radians, by the way.
|
||||||
|
|
||||||
|
-- Calls with one string param don't need parens:
|
||||||
|
print 'hello' -- Works fine.
|
||||||
|
````
|
||||||
|
|
||||||
|
## Advanced
|
||||||
|
|
||||||
|
### Tables/array
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
#### Metatables and metamethods
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
####
|
41
support/while.lua
Normal file
41
support/while.lua
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
-- Jeb while loop
|
||||||
|
local jebSum = 0
|
||||||
|
print('jeb : '..jebSum)
|
||||||
|
while jebSum < 10 do
|
||||||
|
jebSum = jebSum + 1
|
||||||
|
print('jeb : '..jebSum)
|
||||||
|
end
|
||||||
|
print('\n')
|
||||||
|
|
||||||
|
-- Rob for loop
|
||||||
|
for i = 1, 10 do
|
||||||
|
print('Rob : '..i)
|
||||||
|
end
|
||||||
|
print('\n')
|
||||||
|
|
||||||
|
-- Tim for loop
|
||||||
|
for i = 0, 20, 2 do
|
||||||
|
print('Tim : '..i)
|
||||||
|
end
|
||||||
|
print('\n')
|
||||||
|
|
||||||
|
-- Clara for loop
|
||||||
|
for k,v in pairs({'hi', 'hello', 'smart', 'and', 'or', 'if', 'add', 'die', 'negatif', 'positive'}) do
|
||||||
|
print('Clara : '..k..' '..v)
|
||||||
|
end
|
||||||
|
print('\n')
|
||||||
|
|
||||||
|
-- Mart while loop
|
||||||
|
local martSum = 0
|
||||||
|
repeat
|
||||||
|
print('Mart : '..martSum)
|
||||||
|
martSum = martSum + 1
|
||||||
|
until martSum > 10
|
||||||
|
print('\n')
|
||||||
|
|
||||||
|
-- Breaking out loop
|
||||||
|
while true do
|
||||||
|
print('Breaking out in')
|
||||||
|
if true then break end
|
||||||
|
end
|
||||||
|
print('Breaking out -> Done')
|
Loading…
Reference in New Issue
Block a user