This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function cc_if_truthy(cont, ...) | |
if ... then | |
return cont(...) | |
end | |
end | |
local class | |
class = setmetatable({ | |
meta = { -- Metatable for classes | |
__call=function(self, ...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local size = 1e4 | |
local iterations = 1e4 | |
-------------------------------------------------------------------------------- | |
local function time(iterations, func) | |
local clock = os.clock | |
local before = clock() | |
for i=1,iterations do | |
func() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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, |