Spoiler
OpenComputers has this code:
Quote
proper ternary operators/statements someCondition ? if-true : if-false
function iftxt(cond, tout, fout) if cond then return tout elseif fout~=nil then return fout end end print(iftxt(true,"Yes!","No!")) -- Output: Yes! print(iftxt(false,"Meh.")) -- Won't output anything.
Quote
data type checking function foo(string bar)
Spoiler
local function checkArg(n, have, ...) have = type(have) local function check(want, ...) if not want then return false else return have == want or check(...) end end if not check(...) then local msg = string.format("bad argument #%d (%s expected, got %s)", n, table.concat({...}, " or "), have) error(msg, 3) end end
function foo(bar) checkArg(1, bar, "string") -- Do something end
Quote
Using the example on the linked page:function volume(a, b, c) checkArg(1, a, "number", "nil") checkArg(2, b, "number", "nil") checkArg(3, c, "number", "nil") if type(a)=="number" and type(B)/>/>=="nil" and type(c)=="nil" then return a*a*a elseif type(a)=="number" and type(B)/>/>=="number" and type(c)=="nil" then return 3.14*a*a*b elseif type(a)=="number" and type(B)/>/>=="number" and type(c)=="number" then return a*b*c end print(volume(10)) print(volume(2.5, 8)) print(volume(100, 75, 15)) end
Quote
default parameter values function foo(string bar = "none")
function foo(bar) if bar==nil then bar = "none" end -- Do something end
Quote
binary numbers b10011000
function binary(seq) local num = 0 local cnt = 1 seq = string.reverse(seq) for plc = 1, string.len(seq) do local chr = string.sub(seq, plc, plc) if chr == "1" then num = num + cnt elseif chr ~= "0" then error("Not a binary sequence.") end cnt = cnt * 2 end return num end print(binary("10011000"))
Quote
a few random metamethods, such as __type
oldtype = type type = function(obj) if obj.__type then return obj.__type end return oldtype(obj) end
Quote
a better assert function, preferably one like I made all that time ago that has support for the throwback levels!
[Insert the one you made all that time ago here]