Skip to content

Instantly share code, notes, and snippets.

View justinmeiners's full-sized avatar

Justin Meiners justinmeiners

View GitHub Profile
; syntax objects are true AST nodes, so they are an algebraic data type with data constructors and deconstructors:
;
; syntax = (syntax-null props) | (syntax-cons props syntax syntax) | (syntax-identifier props symbol) | (syntax-atom props datum)
;
; the constructors have dual deconstructors with names like syntax-car, syntax-cdr, syntax-identifier-symbol,
; and type predicates like syntax-null?, syntax-pair?, syntax-identifier?.
;
; props is just an open-ended plist of key-value pairs with optional information about things like source location
; and anything else you want to track. you can call syntax-props on any of the syntax variants to get the plist.
; as a convenience when calling the constructor functions, you can either pass in a props plist directly, or you can
@pervognsen
pervognsen / btree.inl
Last active June 15, 2019 17:23
Experiments in lightweight C templates
// Here is btree.inl, which is the thing you would write yourself.
// Unlike C++ templates, the granularity of these lightweight templates is at the
// module level rather than the function or class level. You can think of it like
// ML functors (parameterized modules) except that there isn't any static checking
// of signatures (in that respect, it's like C++ templates). In my view, this style
// of parameterized generative modules is generally the better conceptual framework.
// This is a completely valid C file even prior to preprocessing, so during library
// development you can just include this file directly. That is a big win for testing
@pervognsen
pervognsen / templatize.py
Last active June 16, 2019 03:35
Lightweight C template preprocessor
# template.py btree.inl => btree.h, btree.out.inl
import re
import sys
import os
import os.path
if len(sys.argv) != 2:
print "Usage: template.py <filename>"
sys.exit(1)
# template.py btree.inl => btree.h, btree.out.inl
import re
import sys
import os
import os.path
if len(sys.argv) != 2:
print "Usage: template.py <filename>"
sys.exit(1)
/* Lightweight module based templates in standard C
===================================================
This is a proof of concept example of lightweight module based templates in C and
is loosely based on https://gist.github.com/pervognsen/c56d4ddce94fbef3c80e228b39efc028 from Per Vognsen.
While his approach (at least as far as I understood) is based on a python script to generate
the .h/.c files for you is this implementation contained in one single header file.
This is an outline to show how you can use the single header approach
and bend it to its absolute extrems. I tend to write specialized datastructures
for most of my problems but sometimes it happens that I have to use a particular

The trick to designing transpose algorithms for both small and large problems is to recognize their simple recursive structure.

For a matrix A, let's denote its transpose by T(A) as a shorthand. First, suppose A is a 2x2 matrix:

    [A00 A01]
A = [A10 A11]

Then we have:

@bxt
bxt / proxy.py
Last active August 30, 2020 15:57
A very basic caching python HTTP proxy server.
# Originally from http://sharebear.co.uk/blog/2009/09/17/very-simple-python-caching-proxy/
#
# Usage:
# A call to http://localhost:80000/example.com/foo.html will cache the file
# at http://example.com/foo.html on disc and not redownload it again.
# To clear the cache simply do a `rm *.cached`. To stop the server simply
# send SIGINT (Ctrl-C). It does not handle any headers or post data.
import BaseHTTPServer
import hashlib

I've been reading this much-publicized paper on neural ordinary differential equations:

https://arxiv.org/abs/1806.07366

I found their presentation of the costate/adjoint method to be lacking in intuition and notational clarity, so I decided to write up my own tutorial treatment. I'm familiar with this material from its original setting in optimal control theory.

You have a dynamical system described by an autonomous first-order ODE, x' = f(x), where the state x belongs to an n-dimensional vector space. There is a value function V(x) defined over the state space. Given a particular path t -> x(t) satisfying the ODE, we may evaluate it at the terminal time T to get the terminal state x(T) and the terminal value V(x(T)).

@chaoxu
chaoxu / word_problem_braid_group.hs
Created June 23, 2011 05:54
Word Problem for Braid Group
default (Int, Integer, Rational, Double)
if' :: Bool -> a -> a -> a
if' True x _ = x
if' False _ y = y
isBraidIdentity x n =
[1..n] == concat (map (braidReduce x) [[y] | y <- [1..n]])
braidReduce = flip (foldr homomorphism)
@frankus
frankus / Pure functions.md
Last active April 29, 2021 16:50
Extracting Pure Functions in Swift Without Polluting the Global Namespace

For testability purposes (and to make things easier to reason about), sometimes it's nice if methods in a class are pure functions, that is, they don't contain any explicit or implicit references to self, or really anything other than the arguments that are passed in.

In the painfully contrived example below, method is a method (because it references both a property and another method using self) and isEven is a pure function (because it doesn't).

class MyClass {
  let number: Int
  
 init(number: Int) {