Skip to content

Instantly share code, notes, and snippets.

View Validark's full-sized avatar
🍳

Niles Salter Validark

🍳
View GitHub Profile
@Validark
Validark / autocomplete.lua
Created June 27, 2021 17:57
A simple autocomplete proof of concept in Lua which binary searches a sorted array of strings. Also allows for searching for terms with a different word order than the original string (by inserting permutations into array) and permitting alternate spellings/abbreviations by permuting those as well.
-- A cool autocomplete demo
-- @author Validark
-- Strings are stored in a lexigraphically sorted array, which can be binary searched for the first and last element which matches a query
-- Please note the "groupings" functionality is an unvalidated afterthought which may or may not work properly, but overall this code has some nice gems:
-- I was originally thinking that https://github.com/evaera/Cmdr could use this to get O(log n) autocompletes (old algo uses O(n))
-- However, Cmdr is designed in such a way that it creates a new autocomplete function on-demand each time,
-- which means we'd have to sort (or at least verify the sortedness) of the most recent data each time (for changing data at least, like a Player list).
-- Sorting at run-time takes O(n*log n) time, making it hard to compete with the old algorithm where the pre-processing step just gets the data in the right format
@Validark
Validark / regexBacktrackingTests.lua
Created March 1, 2021 08:19
This monstrosity is a giant test bench for figuring out which regular expression-based functions take linear or quadratic time. It can also be used just for performance comparisons
local R1 = 2^10
local R2 = 2^11 -- the size of strings you want to test
local find = string.find
local sub = string.sub
local gsub = string.gsub
local match = string.match
local gmatch = string.gmatch
local byte = string.byte
local candidates = {
@Validark
Validark / commify.lua
Last active December 26, 2020 20:20
Takes in a number or a string coercible to a number, and returns a string where every 3rd digit from the decimal point is separated by a comma
-- Takes in a number or a string coercible to a number, and returns a string where every 3rd digit from the decimal point is separated by sep (default = ",")
-- e.g. 1000 -> 1,000 and 1234567 -> 1,234,567
-- scales linearly, basically O(2n), and doesn't create any unnecessary intermediary strings
-- @author Validark
local function commify(num, sep)
local t = type(num)
if t == "number" then
num = string.format("%f", num)
elseif t ~= "string" then
const HttpService = game.GetService("HttpService");
export async function getRequest(...[url, nocache, headers]: Parameters<HttpService["GetAsync"]>) {
return HttpService.GetAsync(url, nocache, headers);
}
export interface ReferenceData {
readonly owner: string;
readonly repository: string;
readonly branch?: string;
@Validark
Validark / Grapher.lua
Created June 21, 2020 12:33
Roblox Studio grapher.lua
-- ugh.lua
local function foo1(...)
local result = {}
for i = 1, select("#", ...) do
result[i] = (select(i, ...)) .. "b"
end
return result
end
local RunService = game:GetService("RunService")
local delay do
-- uses a sorted singly linked list (queue) to achieve O(1) remove operations and O(n) for insert
local first -- the initial node in the linked list
local connection -- the Heartbeat `RBXScriptConnection | nil`
function delay(seconds, resolve)
assert(type(seconds) == "number", "Bad argument #1 to delay, must be a number.")
interface BinaryOperation {
symbols: ReadonlyArray<string>;
call: (a: number, b: number) => number;
precedence: number;
leftAssociative: boolean;
}
// function* parseStr(str: string) {
// let lastIndex = 0;
// const reg = /(?<whitespace>\s+)|(?<hex>0x[0-9A-Fa-f]+\.?[0-9A-Fa-f]*)|(?<octal>0o[0-7]+\.?[0-7]*)|(?<binary>0b[01]+\.?[01]*)|(?<decimal>\d+\.?\d*)|(?<binaryOperator>[-\+\*/])/gu;
-- From Wikipedia @https://en.wikipedia.org/wiki/Xiaolin_Wu's_line_algorithm
-- Xiaolin Wu's line algorithm
local StarterGui = game:GetService("StarterGui")
StarterGui:ClearAllChildren()
local Screen = Instance.new("ScreenGui", StarterGui)
local Triangle = Instance.new("Frame", Screen)
Triangle.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
-- code compiled by Roblox-TS
local _exports;
local getSignificantDigits = function(n)
local str = tostring(n);
if tonumber(str) == n then
return str;
end;
for i = 15, 99 do
str = ("%." .. tostring(i) .. "g"):format(n);
if tonumber(str) == n then
@Validark
Validark / Event.lua
Created January 30, 2019 06:07
Extremely light-weight Event object
-- The lightest Event library ever
-- @author Validark
-- Micro-optimizations
local coroutine_yield = coroutine.yield
local coroutine_resume = coroutine.resume
local coroutine_create = coroutine.create
local coroutine_running = coroutine.running
local Event = {}