Skip to content

Instantly share code, notes, and snippets.

View lambda-fairy's full-sized avatar

Chris Wong lambda-fairy

View GitHub Profile
@lambda-fairy
lambda-fairy / counter.py
Last active December 21, 2015 10:18
Count the number of comparisons performed by a program
"""
A simple mixin that counts the number of comparisons performed. Each
class it is mixed into keeps its own counter, which can be retrieved and
reset at whim. The code is written generically, and will work with any
orderable type.
In the COSC122 (first year computer science) course at the University of
Canterbury, one of the assignments is to implement a binary search. To
verify the algorithm, students have to manually increment a counter
every time they compare two values. This is annoying and error-prone,
@lambda-fairy
lambda-fairy / boygirl.py
Created August 14, 2013 03:38
Monte Carlo simulation of "Boy or Girl" paradox
#!/usr/bin/env python3
"""
I go around, knocking on people's doors, until I find someone with two
children, at least one of which is a boy.
What is the probability that they are both boys?
(The answer should approximate 1/3.)
@lambda-fairy
lambda-fairy / Comb.hs
Created August 12, 2013 06:19
Simple parser combinator library
-- | A simple parser combinator library.
module Text.Comb
( Parser(runParser)
, parse
, next
, satisfy
, match
, matches
) where
@lambda-fairy
lambda-fairy / api.txt
Last active December 20, 2015 22:18
Sparkle API
GET /project
200 OK
{
"revision": revision,
"tasks": [
{
"title": string,
"completed": boolean,
"children": [ .. ]
@lambda-fairy
lambda-fairy / .bashrc
Last active December 20, 2015 14:09
My .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# Include the default .bashrc
. /etc/skel/.bashrc
# Don't save command history on the VM
if [ "$(hostname)" == 'bonbon' ]; then
unset HISTFILE
@lambda-fairy
lambda-fairy / Names.hs
Created July 25, 2013 01:55
Python identifier generator
module Names (renderName) where
import Data.List (genericIndex)
-- | Map a non-negative integer to a valid Python identifier.
renderName :: Integer -> String
renderName
= ('_':) -- Prevent conflict with Python keywords
. map (genericIndex chars)
. digitify (fromIntegral $ length chars)
@lambda-fairy
lambda-fairy / log.c
Last active December 19, 2015 18:18
Simple logging system in C
#include <stdio.h>
#include "log.h"
void
PLog_printf(const char *type, const char *filename, unsigned int line, const char *fmt, ...) {
va_list vl;
va_start(vl, fmt);
fprintf(stderr, "%s:%u: %s: ", filename, line, type);
vfprintf(stderr, fmt, vl);
putc('\n', stderr);
@lambda-fairy
lambda-fairy / PeanoAxioms.agda
Last active December 19, 2015 17:09
Peano axioms in Agda
-- http://en.wikipedia.org/wiki/Peano_axioms
module PeanoAxioms where
data ⊥ : Set where -- empty
data ℕ : Set where
-- [1] 0 is a natural number.
zero : ℕ
-- [6] For every natural number n, S(n) is a natural number.
@lambda-fairy
lambda-fairy / names.txt
Created July 9, 2013 06:00
A whole bunch of generated variable names
__
_a
_b
_c
_d
_e
_f
_g
_h
_i
@lambda-fairy
lambda-fairy / scott.py
Last active December 19, 2015 10:39
Scott encoding in Python
"""Great Scott!"""
import sys
sys.setrecursionlimit(4000)
fix = lambda f: lambda x: f(fix(f))(x)
zero = lambda z: lambda s: z
succ = lambda x: lambda z: lambda s: s(x)
# plus, times are faster when the second argument is small