Skip to content

Instantly share code, notes, and snippets.

@aprell
aprell / bf.cpp
Last active October 8, 2015 16:21
BF interpreter
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cassert>
#define TAPE_SIZE 20000
static unsigned char tape[TAPE_SIZE] = {0};
static unsigned char *ptr = tape;
@aprell
aprell / qtest.sh
Created October 2, 2015 18:35
Wrapper for qtest (OCaml)
#!/bin/bash
test=test_${1%.*}
qtest -o $test.ml extract $1
ocamlbuild -cflags -warn-error,+26 -use-ocamlfind -pkg oUnit,QTest2Lib $test.native
./$test.native
rm -f qtest.targets.log $test.ml $test.native
rm -rf _build
@aprell
aprell / meta.c
Created August 16, 2015 17:52
Advanced metaprogramming in C http://250bpm.com/blog:56
#include <stdio.h>
#define concat_(x, y) x##y
#define concat(x, y) concat_(x, y)
#define add_int(hd, val) \
node_t concat(hd_, __LINE__) = { .i = val, INT, hd }; \
hd = &concat(hd_, __LINE__)
#define add_float(hd, val) \
@aprell
aprell / merlin-init.sh
Created July 11, 2015 08:50
Create .merlin for an OCaml project
#!/bin/sh
if [ -f .merlin ]; then
echo ".merlin already exists"
read -p "Overwrite? (y/n) " -n 1
echo
if [[ $REPLY =~ [Nn] ]]; then
echo "Okay, bye."
exit 1
fi
@aprell
aprell / compose.hs
Created July 4, 2015 19:58
Function composition in Standard ML, OCaml, and Haskell
twice f = f . f
n `times` f = if n > 0 then f . ((n-1) `times` f) else id
import Control.Monad (guard)
carnac :: [String]
carnac = do
o1 <- ops
o2 <- ops
o3 <- ops
o4 <- ops
o5 <- ops
o6 <- ops
import Control.Monad (guard, mfilter)
import Control.Monad.Trans.State
import Data.List (foldl')
select :: [a] -> [(a, [a])]
select [] = []
select (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- select xs]
-- StateT select :: StateT [a] [] a
import System.Environment
import Data.List (foldl', intercalate)
import Data.List.Split (splitOn)
addTo :: Num a => [a] -> [a] -> [a]
addTo [] values = values
addTo totals values
| length values == length totals = zipWith (+) values totals
| otherwise = error "Inconsistent number of fields"
@aprell
aprell / sumcsv.sml
Last active August 29, 2015 14:20
Slightly different Standard ML version of http://thebreakfastpost.com/2015/04/22/four-mls-and-a-python
fun toReal str =
case Real.fromString str of
SOME r => r
| NONE => raise Fail ("Invalid number: " ^ str)
fun split line delim = String.tokens (fn c => c = delim) line
fun foldS f acc stream =
case TextIO.inputLine stream of
SOME line => foldS f (f (acc, line)) stream
@aprell
aprell / cleanup.c
Last active August 29, 2015 14:20
Pthread cleanup handler
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
struct handler_stats {
int x, y;
};
static void *handler(void *args)
{