Skip to content

Instantly share code, notes, and snippets.

View tmhedberg's full-sized avatar

Taylor M. Hedberg tmhedberg

View GitHub Profile
@tmhedberg
tmhedberg / pacmanifest
Created August 6, 2011 14:05
Display the number of cached versions for each package in the pacman cache
#!/bin/bash
ls /var/cache/pacman/pkg |
awk -F - '
{
k = ""
for (i = 1; i < NF - 2; ++i)
k = k $i "-"
sub(/-$/, "", k)
@tmhedberg
tmhedberg / fxg.sh
Created October 20, 2011 20:40
Shell function to search specific files for a pattern (augmented grep)
# Common pattern for searching file contents
fxg () {
local OPTIND=1 findbase=. grepflags verbose=false usage=false
while getopts b:g:vh opt; do
case $opt in
b) findbase=$OPTARG ;;
g) grepflags=$OPTARG ;;
v) verbose=true ;;
@tmhedberg
tmhedberg / crawlscores
Created November 1, 2011 04:45
Print Dungeon Crawl Stone Soup high score list
#!/bin/bash
# Print Dungeon Crawl Stone Soup high score list
{
echo ' |Score|Name|Level|Type|Religion|Runes Ascended'
for f in `ls -tr ~/.crawl/morgue/morgue-*.txt`; do
sed -nr '
3{
s/([0-9]+)./\1|/
@tmhedberg
tmhedberg / gist:1345463
Created November 7, 2011 16:40
bash function to provide improved Git status indicator in primary prompt
parse_git_branch () {
local stat
if ! stat=`git status -sb 2>/dev/null` || [[ -n $NO_GIT_PROMPT ]]; then
return
fi
local ref=`sed -nr '/^##/{s/^## (Initial commit on )?//;p}' <<<"$stat"`
local dirty=`
@tmhedberg
tmhedberg / jumpbuf.vim
Created November 18, 2011 15:46
Jump through Vim's jump list one buffer at a time
" Jump backwards or forwards through the jump list one buffer at a time,
" rather than one jump at a time.
"
" Use <Leader>o to jump backwards and <Leader>i to jump forwards
let s:JUMPLIMIT = 100
if exists('g:loaded_jump_buf') && loaded_jump_buf
finish
endif
@tmhedberg
tmhedberg / ChurchBool.hs
Created December 29, 2011 18:17
Church Booleans in Haskell
{-# LANGUAGE FlexibleInstances, Rank2Types, TypeFamilies #-}
module ChurchBool where
type CB = forall a. a -> a -> a
instance a ~ Bool => Show (a -> a -> a) where
show cb = if cb True False then "cTrue" else "cFalse"
cTrue, cFalse :: CB
@tmhedberg
tmhedberg / sdl_test.c
Created February 12, 2012 21:45
Simple SDL graphics test
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(void)
{
SDL_Surface *main_surf;
SDL_Event *const ev = malloc(sizeof (SDL_Event));
@tmhedberg
tmhedberg / THOp.hs
Created March 4, 2012 03:51
Using Template Haskell to declare operators
{-# LANGUAGE TemplateHaskell #-}
module THOp (mkOps) where
import Control.Monad
import Language.Haskell.TH
mkOp :: String -> Q [Dec]
mkOp n = [| $(varE a) + $(varE b) + length (filter (=='-') n) |] >>= \body ->
@tmhedberg
tmhedberg / rep.hs
Created April 7, 2012 23:24
Parallel computation of numbers with no repeated digits in Haskell
{-
- Count how many numbers with no repeated digits lie between 1 and the
- specified maximum (given as a command line argument)
-
- For instance 183957 is counted, while 298387 is not ('8' occurs twice).
-
- On SMP systems, parallelism is exploited to speed up the computation
- significantly. The search space is divided into as many evenly-sized chunks
- as the host system has cores, and worker threads are spawned to run on each
- core. This small program is primarily intended to illustrate the usage of
@tmhedberg
tmhedberg / AOTest.hs
Created May 9, 2012 18:28
Advanced overlapping instance resolution
{-# LANGUAGE FlexibleInstances
, MultiParamTypeClasses
, TemplateHaskell
, TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Prelude hiding (print)
import AdvancedOverlap