Skip to content

Instantly share code, notes, and snippets.

Trevor Caira trevorc

Block or report user

Report or block trevorc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:100173
(defn factorial [n]
(reduce * (range 1 (inc n))))
View gist:100253
(defn evens
([] nil)
([x] nil)
([x y & xs]
(lazy-seq (cons y (apply evens xs)))))
View pk_lookup.py
'''
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:
View ngrams.py
#!/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
Backup MongoDB locally
View mongo-backup.py
#!/usr/bin/env python
import os
import sys
import bisect
import shutil
import datetime
import operator
import subprocess
@trevorc
trevorc / mutex.c
Created Aug 18, 2011
Python segfault on Mac OS X 10.7
View mutex.c
#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 Sep 3, 2011
Compute the first four sample moments about the mean (and the standard deviation) iteratively from a stream of samples
View moments.awk
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]
View Base C++ makefile
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 Oct 17, 2011
Haskell RPN calculator
View rpn.hs
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
@trevorc
trevorc / deg.html
Created Oct 17, 2011
Convert between degrees, minutes, and seconds and decimal degrees
View deg.html
<!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">
You can’t perform that action at this time.