Skip to content

Instantly share code, notes, and snippets.

View tylerneylon's full-sized avatar
😆

Tyler Neylon tylerneylon

😆
View GitHub Profile
@tylerneylon
tylerneylon / .block
Last active May 3, 2024 01:21
Quick js code to draw math functions in an SVG element.
license: mit
@tylerneylon
tylerneylon / rwlock.py
Last active May 2, 2024 12:46
A simple read-write lock implementation in Python.
# -*- coding: utf-8 -*-
""" rwlock.py
A class to implement read-write locks on top of the standard threading
library.
This is implemented with two mutexes (threading.Lock instances) as per this
wikipedia pseudocode:
https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Using_two_mutexes
@tylerneylon
tylerneylon / learn.lua
Last active April 28, 2024 22:23
Learn Lua quickly with this short yet comprehensive and friendly script. It's written as both an introduction and a quick reference. It's also a valid Lua script so you can verify that the code does what it says, and learn more by modifying and running this script in your Lua interpreter.
-- Two dashes start a one-line comment.
--[[
Adding two ['s and ]'s makes it a
multi-line comment.
--]]
----------------------------------------------------
-- 1. Variables and flow control.
----------------------------------------------------
@tylerneylon
tylerneylon / json.lua
Last active April 19, 2024 21:02
Pure Lua json library.
--[[ json.lua
A compact pure-Lua JSON library.
The main functions are: json.stringify, json.parse.
## json.stringify:
This expects the following to be true of any tables being encoded:
* They only have string or number keys. Number keys must be represented as
strings in json; this is part of the json spec.
@tylerneylon
tylerneylon / copy.lua
Last active April 10, 2024 02:29
How to deep copy Lua values.
-- copy.lua
--
-- Lua functions of varying complexity to deep copy tables.
--
-- 1. The Problem.
--
-- Here's an example to see why deep copies are useful. Let's
-- say function f receives a table parameter t, and it wants to
@tylerneylon
tylerneylon / call_graph.awk
Created October 7, 2014 01:08
Parse Lua code and draw its call graph.
#!/usr/bin/awk -f
#
# call_graph.awk
#
# Usage:
# ./call_graph.awk my_program.lua | dot -Tpng > call_graph.png
#
# This is a script that generates a visual call graph
# for a Lua file. This script only shows calls made
# to functions defined within the input Lua file; that is,
@tylerneylon
tylerneylon / Makefile
Created December 3, 2015 23:56
Skeleton Lua C module for teaching.
all: demo.so
clean:
rm *.o *.so
demo.so: demo.o printstack.o
cc -bundle -undefined dynamic_lookup -o demo.so demo.o printstack.o
demo.o: demo.c
cc -fPIC -o $@ -c $< -Ilua_src
@tylerneylon
tylerneylon / splits.py
Created March 3, 2024 20:36
A script to help with the Split Decisions word puzzles in the New York Times
#!/usr/bin/env python3
# coding: utf-8
""" splits.py
Usage:
./splits .in,cr..
This will print out all known words that match both patterns with fixed
letters per dot. In the above example, one answer pair would be:
@tylerneylon
tylerneylon / depth_first_traverse.js
Created August 24, 2023 18:56
A general depth-first traversal utility function in JavaScript
// This is a depth-first traversal function with a few nice features.
//
// * Call this function as depthFirstTraverse(root, tree, fn)
// `tree` is an object whose properties are nodes in a tree; the
// values are arrays of that node's child nodes.
// `root` is the starting point for the traversal; a property in `tree`.
// `fn` is called as in fn(node, depth, childNum) for each node.
// childNum is the index of node as a child of its parent;
// as a special case, the childNum of `root` is undefined.
// `depth` is 0 for the root, and in general indicates how
@tylerneylon
tylerneylon / printTree.js
Last active July 22, 2023 21:04
A cute and simple way to print trees via console.log().
/* printTree.js
*
* A little function to print out a tree via console.log().
*
* The tree is expected to be an object whose keys (aka properties) are
* treated as nodes; each node mapping to an array of its children.
* Leaf nodes don't need to be present as keys.
*
* Here is an example tree with root element 'a':
* t = {a: ['b', 'c'], b: ['d'], c: ['e'], d: ['f', 'g'], g: ['h']}