Skip to content

Instantly share code, notes, and snippets.

View DarkWiiPlayer's full-sized avatar
💭
OwO

Wii DarkWiiPlayer

💭
OwO
View GitHub Profile
@DarkWiiPlayer
DarkWiiPlayer / call_cc.lua
Created March 19, 2020 08:19
An abstraction around callbacks in Lua
-- Simulate an event loop as a simple scheduler.
-- Tasks can schedule other tasks that get executed in order.
local tasks = {insert = table.insert}
function tasks:run()
while #tasks > 0 do
local task = table.remove(tasks, 1)
task()
end
end
@DarkWiiPlayer
DarkWiiPlayer / resty-install.sh
Last active February 3, 2024 11:39
Openresty installation script
#!/bin/bash
# This script installs OpenResty on an ubuntu-like system.
if [ "$(id -u)" -ne 0 ]
then
echo 'must run as root 😖'
exit
fi
umask a+rx
--- Yet another weird idea.
-- Functions, but you can override their next return value by "pushing" a value on them.
-- In this example, the function won't get called until their override stack is empty.
local function wrap(func)
local stack = {}
return function(...) -- A wrapper around the original function
if #stack > 0 then
return table.unpack(table.remove(stack))
else
@DarkWiiPlayer
DarkWiiPlayer / class.lua
Created February 6, 2020 09:10
Having fun with classes
local function cc_if_truthy(cont, ...)
if ... then
return cont(...)
end
end
local class
class = setmetatable({
meta = { -- Metatable for classes
__call=function(self, ...)
local function commas(number)
return tostring(number) -- Make sure the "number" is a string
:reverse() -- Reverse the string
:gsub('%d%d%d', '%0,') -- insert one comma after every 3 numbers
:gsub(',$', '') -- Remove a trailing comma if present
:reverse() -- Reverse the string again
:format() -- a little hack to get rid of the second return value 😜
end
function comma_value(amount)
local size = 1e4
local iterations = 1e4
--------------------------------------------------------------------------------
local function time(iterations, func)
local clock = os.clock
local before = clock()
for i=1,iterations do
func()
@DarkWiiPlayer
DarkWiiPlayer / fin.lua
Last active January 9, 2020 08:52
Function finalizers in Lua
--- Proof of concept implementation of finalizers in Lua
-- FIXME: Tail calls aren't being considered
-- FIXME: When a function calls itself, all of its finalizers would be called at once
local fin
--- Pushes a new finalizer for the current function
local function fin_push(f)
fin = {f=f,n=fin,p=debug.getinfo(2).func}
end
local cosmo = require 'cosmo'
local etlua = require 'etlua'
local lipsum = [[
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mi eget mauris pharetra et ultrices neque. Sed sed risus pretium quam vulputate dignissim suspendisse in. Placerat in egestas erat imperdiet. Diam donec adipiscing tristique risus nec feugiat in fermentum posuere. Etiam tempor orci eu lobortis elementum nibh tellus molestie nunc. Ut etiam sit amet nisl purus in mollis nunc. Porttitor eget dolor morbi non arcu risus quis. Ipsum dolor sit amet consectetur adipiscing. Et tortor consequat id porta nibh venenatis cras sed. Tortor condimentum lacinia quis vel eros donec. Quis imperdiet massa tincidunt nunc pulvinar. Euismod in pellentesque massa placerat. Orci a scelerisque purus semper eget duis at. Mus mauris vitae ultricies leo integer malesuada nunc vel risus. Orci phasellus egestas tellus rutrum tellus. Mauris in aliquam sem fringilla ut morbi. Sit amet aliquam
@DarkWiiPlayer
DarkWiiPlayer / otters.js
Created November 26, 2019 21:39
Just an otter javascript bookmarklet...
let f = function() {
document.querySelectorAll("*").forEach(e=>{Array.prototype.filter.call(e.childNodes,e=>e.nodeType==Node.TEXT_NODE).forEach(e=>e.textContent=e.textContent.replace('other','otter').replace('Other','Otter').replace("OTHER","OTTER").replace('anotter','an otter').replace('Anotter', 'An otter').replace('ANOTTER', 'AN OTTER'))});
};
f();
setTimeout(f, 1000);
setTimeout(f, 2000);
setTimeout(f, 3000);
-- Returns its argument list filtered through fn
local function map(fn, elem, ...)
if elem then
return fn(elem), map(fn, ...)
end
end
-- Takes a function that acts as a loop body
-- and the same values as the generic for loop.
-- Mimics the behavior of a generic for loop,