Skip to content

Instantly share code, notes, and snippets.

View splch's full-sized avatar

Spencer Churchill splch

View GitHub Profile
@spava
spava / progress.py
Last active March 5, 2023 08:09
a simple and useful progress bar in python
import sys
import time
def progress(iterable, length=33):
count = avg = 0
total = len(iterable)
then = time.time()
for it in iter(iterable):
yield it
@GeoffChurch
GeoffChurch / mi_dcg.pl
Last active February 25, 2022 19:47
DCG-ification of mi_list3 from Power of Prolog chapter on meta-interpreters
mi_dcg_clause, [] --> [natnum(0)].
mi_dcg_clause, [natnum(X)] --> [natnum(s(X))].
mi_dcg_clause, [always_infinite] --> [always_infinite].
mi_dcg --> [].
mi_dcg --> mi_dcg_clause, mi_dcg.
%% Original version from https://www.metalevel.at/acomip/ :
mi_ldclause(natnum(0), Rest, Rest).
@GeoffChurch
GeoffChurch / string_rewriting.pl
Last active December 14, 2021 14:46
String rewriting (SRS) in Prolog using DCGs
rewrite(L, R), [Skip] --> [Skip], rewrite(L, R). % Skip the current head.
rewrite(L, R), R --> L. % Match the current prefix and return.
rewrite(Rules) --> {member(L-R, Rules)}, rewrite(L, R). % Try a single rule.
normalize(P) --> P, normalize(P). % If at first you succeed, try, try again.
normalize(P) --> \+P. % P failed so list remains the same.
% Single-step rewrite with {"ab"->"x", "ca"->"y"}:
% ?- phrase(rewrite([[a,b]-[x], [c,a]-[y]]), [a,b,c,a,b,c], Out).
% Out = [a, b, c, x, c] ;
@GeoffChurch
GeoffChurch / lambda.pl
Last active December 14, 2021 00:58
Implementation of call-by-name lambda calculus in Prolog using logic variables as lambda variables
% Implementation of call-by-name lambda calculus in Prolog using logic variables as lambda variables
%
% The grammar is:
% Term ::= Term-Term % abstraction: LHS is function body; RHS is parameter (Y-X instead of λX.Y)
% | Term+Term % application: LHS is function; RHS is argument (F+X instead of (F X))
eval(Y-X, Y-X). % abstractions are left as-is
eval(F+X, Y) :-
copy_term(F,B), % copy before destructive unification of parameter in case F appears elsewhere
eval(B, Y0-X), % eval into what must be an abstraction and unify X with parameter
@CJEnright
CJEnright / gzip.go
Last active July 1, 2024 21:17
Idiomatic golang net/http gzip transparent compression, an updated version of https://gist.github.com/bryfry/09a650eb8aac0fb76c24
package main
import (
"net/http"
"compress/gzip"
"io/ioutil"
"strings"
"sync"
"io"
)
@raxoft
raxoft / xorshift.asm
Created May 22, 2015 07:36
Fast quality Xor-Shift random number generator for Z80 I have created to weed out the crap RNGs out there. 28 bytes, 122 T cycles, period 2^32-1.
; 8-bit Xor-Shift random number generator.
; Created by Patrik Rak in 2008 and revised in 2011/2012.
; See http://www.worldofspectrum.org/forums/showthread.php?t=23070
org 40000
call rnd ; BASIC driver
ld c,a
ld b,0
ret
@darccio
darccio / pearson-hashing.c
Last active April 11, 2023 13:14
Pearson hashing (just for fun). Includes Ruby and Golang versions for RFC 3074 and original variants.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
/*
* Pearson hashing (from Wikipedia)
*
* Pearson hashing is a hash function designed for fast execution on processors with 8-bit registers.
* Given an input consisting of any number of bytes, it produces as output a single byte that is strongly
* dependent on every byte of the input. Its implementation requires only a few instructions, plus a