Skip to content

Instantly share code, notes, and snippets.

View mndrix's full-sized avatar

Michael Hendricks mndrix

View GitHub Profile
@mndrix
mndrix / lexer.mli
Created July 30, 2023 17:07
Lexical analysis with OCaml effect handlers
type t = Lexing.lexbuf -> Parser.token
val of_file : string -> t * Lexing.lexbuf
(** [of_file path] returns a new lexer for extracting tokens from
a file at [path]. *)
@mndrix
mndrix / base32-padding.go
Created November 2, 2015 21:54
Add padding characters to base32 encoded data
package main
import (
"encoding/base32"
"fmt"
"strings"
)
func main() {
data := "2246b2egzcc3ktvvoklo5cvzh4"
@mndrix
mndrix / main.go
Created November 18, 2014 20:21
Golang abort HTTP request after header
package main
import (
"crypto/tls"
"fmt"
"net/http"
"time"
)
// Make a GET request for a large (24 MB) file. We only care about a single header field ("ETag")
@mndrix
mndrix / build-gnatcoll-bindings
Last active November 13, 2021 17:48
Scripts for building GNAT, et al on OpenBSD
#/bin/sh
#
# Build gnatcoll-bindings from source.
#
# Make sure that the first element of PATH is /usr/local and the
# second element is the GNAT toolchain you want to use.
set -e
usage() {
echo "usage: build-gnatcoll-bindings gnatcoll-bindings-src"
@mndrix
mndrix / example.txt
Created January 26, 2013 21:33
Prolog definition of infinite Fibonacci sequence
?- fibs(F), take(F, 20, _).
F = [0, 1, 1, 2, 3, 5, 8, 13, 21|...],
freeze(_G2395, zip_with(plus, [2584, 4181|_G2395], [4181|_G2395], _G2395)).
@mndrix
mndrix / irc.go
Last active June 23, 2020 17:21
Family IRC server
// My small, family IRC server
//
// This file won't compile by itself because it's only one file from
// my larger family server (movie hosting, Asterisk dialplan, Git
// hosting, personal assistant, etc).
//
// Users authenticate via NICK and PASS. The USER is interpreted as a
// "device" name. That allows each user to connect from multiple
// devices simultaneously while still appearing as only one nick in
// channels. Each nick-device combo has a queue of messages which
@mndrix
mndrix / sms.go
Last active June 23, 2020 17:20
SMS over IRC
// A proxy for sending/receiving SMS via IRC
//
// This code is part of our family IRC server whose code is available at
// https://gist.github.com/mndrix/7947009178e4a18c247b4bd25821661f
//
// This file won't compile by itself because it's only one file from
// my larger family server (movie hosting, Asterisk dialplan, Git
// hosting, personal assistant, etc).
//
// Copyright 2018 Michael Hendricks
@mndrix
mndrix / dcg_util.pl
Last active September 29, 2018 17:41
Prolog DCG utility predicates
% at_least//2 is like at_least//3 but ignores the specific matches found.
:- meta_predicate at_least(+,3,*,*).
at_least(N,Goal) -->
at_least(N,Goal,_).
% at_least(N,Goal,Matches) consumes at least N matches of Goal.
% after that, it consumes as much as possible.
:- meta_predicate at_least(+,3,?,*,*).
at_least(N0,Goal,[X|Xs]) -->
@mndrix
mndrix / tips.md
Last active August 14, 2018 04:38
Prolog macros tips

Tips for writing macros in SWI Prolog. This should eventually be expanded into a full blog post:

Only expand macros when requested

For each module that defines a macro, define a predicate like:

wants_my_fancy_macro :-
    prolog_load_context(module, Module),
 Module \== my_module, % don't expand macros in our own code
@mndrix
mndrix / git-commit-cycles.pl
Created June 23, 2009 18:22
Locating Git commit graph cycles
#!/usr/bin/perl
use strict;
use warnings;
use Graph;
use List::MoreUtils qw( any );
my $g = Graph->new;
open my $git, '-|', q{git log --all --format='%H;%P'} or die;
print "Building commit graph ...\n";
while ( my $line = <$git> ) {