Skip to content

Instantly share code, notes, and snippets.

View bdw's full-sized avatar
💭
Hacking

Bart Wiegmans bdw

💭
Hacking
View GitHub Profile
@bdw
bdw / test-cmp-i.nqp
Created November 24, 2014 15:09
which is better (both are corect)
#!/usr/bin/env nqp-m
my int $i := 0;
sub foo (int $x) {
if (nqp::cmp_i($x, 50) > 0) {
nqp::say("$x: OH HAI");
} else {
nqp::say("$x: OH NOES");
}
}
@bdw
bdw / docker-rm-images.sh
Created November 24, 2014 16:09
docker remove images and containers
#!/bin/bash
for id in `docker ps -a | cut -c 1-20 | grep -v 'CONTAINER ID'`; do docker rm -f $id; done
if [ "$1" = "-a" ]; then
for id in `docker images | cut -c 41-60 | grep -v 'IMAGE ID'`; do docker rmi -f $id; done
fi
@bdw
bdw / lvalue.c
Created April 29, 2015 19:42
lvalue demonstration
int * foo(void) {
int a = 4;
return &a;
}
int main() {
int a[5];
a[1] = 3; /* we assign something to the location a[1] */
printf("%d\n", a[0]); /* and now we read from the (uninitialized) location a[0] */
@bdw
bdw / explanation.md
Last active August 29, 2015 14:27
Pathological grammar case

Why this is a problem

I need to generate a transition state table. To do that, I find the combinations of rules that can be applied to a given rule. Before I can do that, note that nested rules are replaced by 'accented' values:

(tile: at (a (t)) r) becomes
(tile: at2 (t) t')
(tile: at (a t') r)

Hence, according to this, the (t) node can generate both r and t', and this is indistinguishable form the perspective of the (t) node.

@bdw
bdw / rulesets.pl
Created August 13, 2015 12:27
ruleset-generation.pl
#!/usr/bin/perl
use Data::Dumper;
use strict;
use warnings;
use sexpr;
sub sortn {
sort { $a <=> $b } @_;
}
@bdw
bdw / Dictionary.php
Created January 7, 2013 21:42
PHP on-disk dictionary. It stores a key-value hash as a binary tree (depth-first), with a 'right offset pointer'. A long time ago, I had methods that manipulated this on-disk, but unfortunately, I don't anymore. I will figure out how, though, and rewrite this into a more honest language.
<?php
class Dictionary {
var $file;
var $index;
var $current;
function Dictionary($file) {
$this->file = $file;
$this->index = array();
@bdw
bdw / tree.go
Last active December 10, 2015 19:58
Binary tree in Go
package main
import (
"fmt"
"strconv"
"io"
"encoding/binary"
)
type BinaryTree struct {
import random
import io
import time
def profile(func):
def wrapper(*args):
start = time.clock()
rv = func(*args)
end = time.clock()
@bdw
bdw / life.py
Created May 28, 2013 21:54
Game of Life met pygame en numpy.
#!/usr/bin/env python
import argparse
import numpy
import pygame
pygame.init()
from pygame.locals import QUIT # Dat deze uit z'n namespace moet is echt stom
# 3 regels:
# 1. Levende cellen met minder dan 2 levende buren gaan dood
@bdw
bdw / volume.js
Created December 26, 2013 16:55
function to calculate volume contained in the 'pools' in a two-dimensional array.
function calculateVolume(heights) {
var left = 0, right = 1, volume = 0;
while (right < heights.length) {
var level = heights[left];
if (heights[right] <= level) {
// tag the 'left cursor' along
left = right;
right = right + 1;
} else {