Skip to content

Instantly share code, notes, and snippets.

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
(defn factorial [n]
(reduce * (range 1 (inc n))))
(defn evens
([] nil)
([x] nil)
([x y & xs]
(lazy-seq (cons y (apply evens xs)))))
'''
Shorthand decorator for the common case of looking up ORM objects from
named URL parameters.
For each named URL parameter passed to the view of the form
``db_table_id``, look up the ORM object in the corresponding model with
the given primary key, returning a 404 in case it is not found.
For example:
#!/usr/bin/env python
import collections
import sys
def tokens(f):
for line in f:
for t in line.split():
yield t
@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
@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 / 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]
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 / 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