Skip to content

Instantly share code, notes, and snippets.

@wagyourtail
Last active June 29, 2023 20:20
Show Gist options
  • Save wagyourtail/aaf8b85e856927e2f21ed35543a57e9e to your computer and use it in GitHub Desktop.
Save wagyourtail/aaf8b85e856927e2f21ed35543a57e9e to your computer and use it in GitHub Desktop.
Scripting Language Cheat Sheet

Variables

JS
// higher scoping, not killed by exit from for/if and some other flow control stuff
var x = 1;

// lower scoping, killed by exit from for/if
let y = 1;

// lower scoping, and no re-definition
const z = 1;
Python
# just one, scoping depends on where it's defined 
# and if you declare it global at the top of stuff like "def"

x = 1
Lua
-- globally scoped
x = 1

-- scoped into the current context
local y = 1

Basic Types

JS
// number
1
1.1

// string
"hello"
'hello'

// template string
`hello ${name}`

// boolean
true
false

// null
null
undefined

// object
{a: 1, b: 2}
// insert
obj.c = 3
// delete
delete obj.c
// access
obj.c

// array
[1, 2, 3]
// insert
arr.push(4)
// delete
arr.pop()
// access
arr[0]
Python
# number
1
1.1

# string
"hello"
'hello'

# format string
"hello {}".format(name)
f"hello {name}"

# boolean
True
False

# null
None

# map
{"a": 1, "b": 2}
# insert
obj["c"] = 3
# delete
del obj["c"]
# access
obj["c"]

# list (array)
[1, 2, 3]
# insert
arr.append(4)
# delete
arr.pop(index)
  # or
del arr[index]
# access
arr[0]
Lua
-- number
1
1.1

-- string
"hello"
'hello'

-- format string
string.format("hello %s", name)

-- boolean
true
false

-- null
nil

-- table (object)
{a = 1, b = 2}
-- insert
obj.c = 3
  -- or
obj["c"] = 3
-- delete
obj.c = nil
-- access
obj.c

-- integer key'd table (array)
-- technically, lua has no arrays, it's just tables with only integer keys
-- do note lua stuff is 1-indexed, not 0-indexed
{1, 2, 3}
-- insert
table.insert(arr, 4)
  -- or
arr[#arr + 1] = 4
-- delete
table.remove(arr, index)
-- access
arr[1]

Flow Control (if statements and boolean stuff)

JS
if (a === b) {
  // code here
} else if (a < b) {
  // code here
} else {
  // code here
}

// also you can detect if an object has a key
if (a in b) {
  // code here
}

// switch statement
switch (a) {
  case 1:
    // code here
    break;
  case 2:
    // code here
    break;
  default:
    // code here
    break;
}
Python
if a == 1:
  # code here
elif a == 2:
  # code here
else:
  # code here

# detect if key is in dict or a member of list/set
if a in b:
  # code here

# as of python 3.10+ you can also use match statemnts
match a:
    case 1:
        # code here
    case 2, 3:
        # code here
    case _:
        # default code here
Lua
if a == 1 then
  -- code here
elseif a == 2 then
  -- code here
else
  -- code here
end

-- detect if key is in dict or a member of list/set
if b[a] ~= nil then
  -- code here
end

-- lua doesn't have switch statements, but you can use a table of functions 
--  (you can refer to functions section below)
local cases = {
    [1] = function()
        -- code here
    end,
    [2] = function()
        -- code here
    end,
    [3] = function()
        -- code here
    end
}

if (cases[a] ~= nil) then
    cases[a]()
else
    -- default code here
end

For Loops

JS
// loop a count number of times
for (let i = 0; i < 10; ++i) {
  // code here
}

// loop over elements in an array
for (const element of array) {
  // code here
}

// loop over keys in an object
for (const key in object) {
  // code here
}
Python
# loop a count number of times
for i in range(10):
    # code here
else:
    # code here run if loop never calls "break"

# loop over elements in an array
for element in array:
    # code here
else:
    # code here run if loop never calls "break"

# loop over keys in an object
for key in object:
    # code here
else:
    # code here run if loop never calls "break"


# loop over elements in an array with index
for i, element in enumerate(array):
    # code here
else:
    # code here run if loop never calls "break"

# loop over key,value pairs in an object
for key, value in object.items():
    # code here
else:
    # code here run if loop never calls "break"
Lua
for i = 1, 10 do
    -- code here
end

-- loop over elements in an array
for i, element in ipairs(array) do
    -- code here
end

-- loop over keys in an object
for key, value in pairs(object) do
    -- code here
end

While Loops

JS
// loop while a condition is true
while (a < b) {
  // code here
}
Python
# loop while a condition is true
while a < b:
    # code here
else:
    # code here run if loop never calls "break"
Lua
while a < b do
    -- code here
end

Do While Loops

JS
do {
  // code here
} while (a < b);
Python Python doesn't have native do-while loops so we have to emulate this behavior
# loop while a condition is true
loopCondition = True
while loopCondition:
    # code here
    loopCondition = a < b;
Lua
repeat
    -- code here
until (a < b)

Functions

JS
// function declaration
function myFunction(a, b) {
  // code here
}

// anonymous function declaration, 
//  (you can put these in other places besides setting them to a variable)
var myFunction = function(a, b) {
  // code here
}

// arrow function
var myFunction = (a, b) => {
  // code here
}

// arrow function with implicit return (returns a + b)
var myFunction = (a, b) => a + b;
Python
# function declaration
def myFunction(a, b):
    # code here

# anonymous function declaration, 
#  (you can put these in other places besides setting them to a variable)
myFunction = lambda a, b: a + b
Lua
function myFunction(a, b)
    -- code here
end

-- anonymous function declaration, 
--  (you can put these in other places besides setting them to a variable)
local myFunction = function(a, b)
    -- code here
end

Classes

JS
// class declaration
class MyClass {
  // code here
  constructor(a, b) {
    // code here
  }
  
  // method declaration
  doSomething(a, b) {
    // code here
  }
}

const a = new MyClass(1, 2);
a.doSomething(3, 4);
Python
# class declaration
class MyClass:
    # code here
    def __init__(self, a, b):
        # code here
    def doSomething(self, a, b):
        # code here
Lua
-- this is technically just a table, but if you refer to the functions with
-- : instead of . it'll pass itself as the first arg
local MyClass = {
    __init__ = function(self, a, b)
        -- code here
    end,
    doSomething = function(self, a, b)
        -- code here
    end
}

function MyClass:new()
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o:__init__(1, 2)
    return o
end

local a = MyClass:new()
a:doSomething(3, 4)

if you think this is a bit confusing, you should see https://www.lua.org/pil/16.1.html

IF YOU THINK OF ANYTHING ELSE I SHOULD ADD, PLEASE LET ME KNOW!

@big-lip-bob
Copy link

Lua doesn't need parentheses around conditions and the repeat until loop in Lua breaks if the condition is true instead so you'd need to invert it so that it stays equivalent to the others.
Also picky topic, OOP in Lua, personally i prefer setting the __index to the class once at the class level itself and using __call instead of :new. Still nice one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment