Skip to content

Instantly share code, notes, and snippets.

@trevorc
trevorc / defwith.clj
Created March 23, 2012 19:24
defwith -- sugar for try-finally macros
(defmacro defwith [name finalizer]
(let [bindings-sym (gensym "bindings")
body-sym (gensym "body")]
`(defmacro ~name [~bindings-sym & ~body-sym]
(cond
(empty? ~bindings-sym) `(do ~@~body-sym)
(symbol? (~bindings-sym 0))
`(let ~(subvec ~bindings-sym 0 2)
(try
(~'~name ~(subvec ~bindings-sym 2) ~@~body-sym)
@trevorc
trevorc / ringbuffer.clj
Created March 15, 2012 07:37
Persistent Ring Buffer
(ns com.caira.util.ringbuffer
(:import (clojure.lang Counted IPersistentCollection IPersistentVector
Indexed RT SeqIterator Seqable Util)
(java.util Collection List)))
(defn ^:private iterables-equiv? [^Iterable i1 ^Iterable i2]
(let [it1 (.iterator i1)
it2 (.iterator i2)]
(loop []
(or (not (.hasNext it1))
module Bunrui.Core
( Command
, Metadata(..)
, Opts(..)
, findSourceFiles
, missingDirectories
, readMetadata
) where
import Control.Applicative ((<$>), (<*), (<*>))
bfs :: Tree a -> [a]
bfs t =
concatMap (map rootLabel) $
takeWhile (not . null) $
iterate (concatMap subForest) [t]
@trevorc
trevorc / deg.html
Created October 17, 2011 05:03
Convert between degrees, minutes, and seconds and decimal degrees
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>coordinate calculator</title>
<style>h1 { font-size: 1.1em; }</style>
</head>
<body>
<h1>Convert between degrees, minutes, and seconds and decimal degrees</h1>
<table id="calc">
@trevorc
trevorc / rpn.hs
Created October 17, 2011 02:24
Haskell RPN calculator
module Main where
import System.Environment
runRPN :: (Fractional a, Read a) => String -> [a]
runRPN = foldl doToken [] . words
where doToken (x:y:zs) "+" = (y+x):zs
doToken (x:y:zs) "-" = (y-x):zs
doToken (x:y:zs) "*" = (y*x):zs
doToken (x:y:zs) "/" = (y/x):zs
CPPFLAGS += -D_BSD_SOURCE -D_XOPEN_SOURCE=600 \
-I/usr/local/include -Isrc
CXXFLAGS += -std=c++98 -fno-rtti -fno-exceptions -pipe \
-Wall -Wextra -Werror -Wcast-align -Wstrict-overflow -Wshadow
LDFLAGS += -L/usr/local/lib
INSTALL ?= install
ifdef pkg_config_libs
CPPFLAGS += $(shell pkg-config --cflags $(pkg_config_libs))
@trevorc
trevorc / moments.awk
Created September 3, 2011 17:34
Compute the first four sample moments about the mean (and the standard deviation) iteratively from a stream of samples
BEGIN { OFS = "\t" }
{
accs[0] += 1
accs[1] += $1
accs[2] += (val2 = $1 * $1)
accs[3] += (val3 = val2 * $1)
accs[4] += (val4 = val3 * $1)
mean = accs[1] / accs[0]
@trevorc
trevorc / mutex.c
Created August 18, 2011 20:12
Python segfault on Mac OS X 10.7
#include <pthread.h>
pthread_mutex_t mutex;
// compile and link with: cc -c mutex.c; ld -dylib -o libmutex.dylib mutex.o -lpthread
@trevorc
trevorc / mongo-backup.py
Created May 27, 2011 14:35
Backup MongoDB locally
#!/usr/bin/env python
import os
import sys
import bisect
import shutil
import datetime
import operator
import subprocess