Lua

From bibbleWiki
Jump to navigation Jump to search

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()