Skip to content

Instantly share code, notes, and snippets.

View tylerneylon's full-sized avatar
😆

Tyler Neylon tylerneylon

😆
View GitHub Profile
@tylerneylon
tylerneylon / learn.lua
Last active April 26, 2025 14:17
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 March 31, 2025 01:08
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 March 5, 2025 13:56
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 / rwlock.py
Last active January 15, 2025 00:16
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 / mnist.py
Last active December 22, 2024 20:15
A function to load numpy arrays from the MNIST data files.
""" A function that can read MNIST's idx file format into numpy arrays.
The MNIST data files can be downloaded from here:
http://yann.lecun.com/exdb/mnist/
This relies on the fact that the MNIST dataset consistently uses
unsigned char types with their data segments.
"""
@tylerneylon
tylerneylon / bigram_freqs.json
Created November 26, 2024 04:00
A short Python script to find bigram frequencies based on a source text.
{"th": 0.03309640905197913, "he": 0.03289597993942979, "pr": 0.002746850084321499, "ro": 0.005424830253317676, "oj": 8.741181560521646e-05, "je": 0.0003752527437597676, "ec": 0.00287222864811888, "ct": 0.002534942652551277, "gu": 0.0005968725994861245, "ut": 0.005476041215995479, "te": 0.009365425536611424, "en": 0.012622619352446206, "nb": 0.00012891035432688487, "be": 0.007778768641231889, "er": 0.023745110677485717, "rg": 0.0005032801504542766, "eb": 0.00018630195043131993, "bo": 0.0023371623828990704, "oo": 0.004796171539066018, "ok": 0.002003408177860971, "of": 0.008637876687533663, "da": 0.0015663490998348887, "av": 0.003626265926167919, "vi": 0.001449800012361267, "id": 0.005190849130738056, "co": 0.006384594329710305, "op": 0.0019883980681105803, "pp": 0.0020528532452740228, "pe": 0.004910071783642512, "rf": 0.0011416512886620695, "fi": 0.0026135249918327343, "ie": 0.003583884439813875, "el": 0.006230961441676894, "ld": 0.005101671419868088, "hi": 0.010678368665954422, "is": 0.009714189851399914, "fo"
@tylerneylon
tylerneylon / animated_svg.html
Created March 17, 2016 20:49
Example of an animated geometric figure
<!DOCTYPE HTML>
<html>
<body>
<svg width="600" height="600" vertion="1.1"
id="svg" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="angleClip">
<polygon id="atriangle" />
</clipPath>
@tylerneylon
tylerneylon / utf8.c
Last active May 27, 2024 16:21
C utf-8 encoder/decoder
// This macro tests if a char is a continuation byte in utf8.
#define IS_CONT(x) (((x) & 0xc0) == 0x80)
// This returns the code point encoded at **s and advances *s to point to the
// next character. Thus it can easily be used in a loop.
int decode_code_point(char **s) {
int k = **s ? __builtin_clz(~(**s << 24)) : 0; // Count # of leading 1 bits.
int mask = (1 << (8 - k)) - 1; // All 1s with k leading 0s.
int value = **s & mask;
// k = 0 for one-byte code points; otherwise, k = #total bytes.
@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 / 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,