Lua: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Introduction= Had a fling with Neovim so needed to know some of this. Half hour video so should be short =Comments= <syntaxhighlight lang="lua"> -- One Line --Multiline -- </syntaxhighlight>" |
No edit summary |
||
Line 8: | Line 8: | ||
Multiline | Multiline | ||
--]] | --]] | ||
</syntaxhighlight> | |||
=Declares= | |||
<syntaxhighlight lang="lua"> | |||
-- Numbers | |||
local number = 5 | |||
-- String | |||
local string = "Fred" | |||
local string = [[ This is | |||
multi line ]] | |||
-- Boolean | |||
local truth, lies = true, false | |||
-- Don't know | |||
local nothing = nil | |||
</syntaxhighlight> | |||
=Functions= | |||
<syntaxhighlight lang="lua"> | |||
-- Nice no semi colons | |||
local; function hello(name) | |||
print("Hello", name) | |||
end | |||
-- We can pass them | |||
local greet = function(name) | |||
-- .. Concat | |||
print("Greeting" .. name .. "!") | |||
end | |||
</syntaxhighlight> | |||
=Data Tables= | |||
==List== | |||
<syntaxhighlight lang="lua"> | |||
local list = {"first", 2 false function() print("fourth") end} | |||
</syntaxhighlight> | |||
==Maps== | |||
<syntaxhighlight lang="lua"> | |||
local t = { | |||
literal_key = "a string", | |||
["an expression"] = "also works", | |||
} | |||
</syntaxhighlight> | |||
=Control= | |||
==For== | |||
<syntaxhighlight lang="lua"> | |||
local fav_acc = {"free", "me", "please"} | |||
for index = 1, #fav_acc do | |||
print(index, fav_acc do[index]) | |||
end | |||
for index, value in ipairs(fav_acc) do | |||
print(index, value) | |||
end | |||
</syntaxhighlight> | |||
==If== | |||
<syntaxhighlight lang="lua"> | |||
if loves_coffee then | |||
blahhh | |||
else | |||
blahhh | |||
end | |||
-- falsey is false or nil all the rest true | |||
</syntaxhighlight> | |||
=Module= | |||
Just like javascript | |||
<syntaxhighlight lang="lua"> | |||
-- bar.lua | |||
local foo = require('foo') | |||
foo.cool_func() | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 23:04, 30 March 2025
Introduction
Had a fling with Neovim so needed to know some of this. Half hour video so should be short
Comments
-- One Line
--[[
Multiline
--]]
Declares
-- Numbers
local number = 5
-- String
local string = "Fred"
local string = [[ This is
multi line ]]
-- Boolean
local truth, lies = true, false
-- Don't know
local nothing = nil
Functions
-- Nice no semi colons
local; function hello(name)
print("Hello", name)
end
-- We can pass them
local greet = function(name)
-- .. Concat
print("Greeting" .. name .. "!")
end
Data Tables
List
local list = {"first", 2 false function() print("fourth") end}
Maps
local t = {
literal_key = "a string",
["an expression"] = "also works",
}
Control
For
local fav_acc = {"free", "me", "please"}
for index = 1, #fav_acc do
print(index, fav_acc do[index])
end
for index, value in ipairs(fav_acc) do
print(index, value)
end
If
if loves_coffee then
blahhh
else
blahhh
end
-- falsey is false or nil all the rest true
Module
Just like javascript
-- bar.lua
local foo = require('foo')
foo.cool_func()