Skip to content

Instantly share code, notes, and snippets.

View sjl's full-sized avatar

Steve Losh sjl

View GitHub Profile
@sjl
sjl / which is best? .py
Created November 16, 2009 20:16 — forked from inky/which is best? .py
They're completely different.
>>> x = '0123456789'
>>> a, b = [{} for _ in x], [{}] * len(x)
>>> a[0]['test'], b[0]['test'] = 1, 1
>>> p(a)
[{'test': 1}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
>>> p(b)
# Discard local changes
git checkout -f (git co -f)
# This will change the current parent revision if you're not at a branch tip.
# The fact that it discards local changes is just a side effect.
hg update --clean (hg co -C)
# What you really want is the command *designed* to throw away changes: revert.
hg revert --all [--no-backup] (hg rev -a)
@sjl
sjl / .cdg.sh
Created April 29, 2010 16:57 — forked from bobthecow/cdg.bash
A function to cd to the root of the current Mercurial repository.
#!/usr/bin/env bash
# cd to the root of the current Mercurial repo
#
# usage:
# Add the following function to your `.bashrc` or `.bash_profile` file,
# or save it somewhere (i.e. `~/.cdg.sh`) and source it in `.bashrc`
cdh () {
hg root >/dev/null && cd "`hg root`"
#!/bin/bash
# Plant rope vim's plugin
# This is a script to install or update 'ropevim'
# Copyright Alexander Artemenko, 2008
# Contact me at svetlyak.40wt at gmail com
function create_dirs
{
mkdir -p src
@sjl
sjl / afacli.py
Created November 1, 2010 19:05
A command-line client for A Feed Apart
import json, time, urllib
n = 0
while 1:
try:
resp = json.load(urllib.urlopen('http://afeedapart.com/u/%s' % n))
except ValueError:
time.sleep(1)
continue
@sjl
sjl / google-jslint.vim
Created December 2, 2010 17:13
Use Google's JSLint with Vim. Now you can use :make when in a .js file to run gjslint on it and get a list of errors in the quickfix window. This was thrown together hastily and Vim's errorformat is confusing as hell, so feel free to fork and improve
au BufNewFile,BufRead *.js set makeprg=gjslint\ %
" The ^G at the end is an actual <Ctrl-G> char.
" Gist won't let me include it, so you'll have to delete the last two characters
" and insert it yourself with <Ctrl-V><Ctrl-G>
au BufNewFile,BufRead *.js set errorformat=%-P-----\ FILE\ \ :\ \ %f\ -----,Line\ %l\\,\ E:%n:\ %m,%-Q,%-GFound\ %s,%-GSome\ %s,%-Gfixjsstyle%s,%-Gscript\ can\ %s,%-G^G
(let [faved (brains.db/user-site-faved-count uid)
mehed (brains.db/user-mehed-count uid)
hated (brains.db/user-hated-count uid)] ...)
(defn personality-score
[faved mehed hated]
(/ (+ faved (* mehed 0.5)) (+ faved mehed hated)))
(defn personality-stddev
[faved mehed hated mean]
(let [points (concat (take faved (cycle [1]))
(take mehed (cycle [0.5]))
(take hated (cycle [0])))
squared (map #(expt (- % mean) 2) points)]
(sqrt (/ (apply + squared) (count squared)))))
(defn personality
[faved mehed hated]
(let [score (personality-score faved mehed hated)]
{ :score score
:stddev (personality-stddev faved mehed hated score)
:total (+ faved mehed hated) }))