Skip to content

Instantly share code, notes, and snippets.

View austintaylor's full-sized avatar

Austin Taylor austintaylor

View GitHub Profile
def process(chicken)
case chicken
when Hen
chicken.collect(&:eggs)
when Rooster
chicken.chop
end
end
#!/bin/bash
# Print out what changed in a nice format.
git log @{1}.. --pretty=format:"%Cblue%an%Creset %ar: %Cred\"%s%b\"%Creset (%h)" --reverse --no-merges
# Print out any new migrations
diff <(ls -l db/migrate/ | grep "[0-9]\{14\}" | sed -e 's/.*\([0-9]\{14\}\).*/\1/g' | sort) <((test -f db/development.sqlite3 && sqlite3 db/development.sqlite3 "select version from schema_migrations" || mysql -uroot `pwd | xargs basename`_development -e "select version from schema_migrations") | grep [0-9] | sort) | grep "< [0-9]\{14\}" | cut -d' ' -f2 | tr '\n' '|' | sed -e 's/\(.*\)|$/"\\\(\1\\\)"/' -e 's/|/\\\|/g' | xargs -J % grep % <(ls -l db/migrate | tail -100) | sed -e 's/.*\([0-9]\{14\}_.*\.rb\)/Pending migration: \1/g'
# Yes, I know about rake db:about_if_pending_migrations, but it's way too slow.
# On the other hand, this makes all kinds of assumptions about your database configuration.
@austintaylor
austintaylor / gist:405513
Created May 18, 2010 20:46
Text object for indented code
" Text object for indented code
onoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR>
onoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR>
vnoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR><Esc>gv
vnoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR><Esc>gv
function! IndTxtObj(inner)
if &filetype == 'haml' || &filetype == 'sass' || &filetype == 'python'
let meaningful_indentation = 1
else
@austintaylor
austintaylor / 1_to_english.rb
Last active September 5, 2015 01:44
Ruby vs. Haskell – to_english
# This is the version I wrote in Ruby. I considered it to be pretty tight code at the time.
# It only handles integers from 0 to 999. When I wrote this, I understood that it ought to
# be more generalized, but I couldn't figure out how to do it in Ruby.
class Integer
ONES = %w(zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen)
TENS = %w(twenty thirty fourty fifty sixty seventy eighty ninety)
def to_english
if self < 20
@austintaylor
austintaylor / gist:465756
Created July 6, 2010 18:39
One-liner to rollback schema changes in a branch
for VERSION in `git diff --summary master..HEAD -- db/migrate/ | sed -e "s/.*migrate\///" -e "s/_.*//" | sort -r` ; do export VERSION && rake db:migrate:down; done
@austintaylor
austintaylor / ShuntingYard.hs
Created April 11, 2011 03:29
A basic math evaluator in Haskell
type Precedence = Int
data Associativity = AssocL | AssocR
data Token = Operand Int | Operator String (Int -> Int -> Int) Associativity Precedence | ParenL | ParenR
instance Show Token where
show (Operator s _ _ _) = s
show (Operand x) = show x
show ParenL = "("
show ParenR = ")"
@austintaylor
austintaylor / event_inspector.js
Created April 12, 2011 18:59
Used this to track down a crazy JS bug one time. Still makes me smile.
var eventColors = {
mouseover: 'red',
mouseout: 'blue',
click: 'yellow'
}
var highlight = function(event) {
Event.stop(event);
element = Event.element(event);
Element.setStyle(element, {backgroundColor: eventColors[event.type]});
@austintaylor
austintaylor / DateSelect.hs
Created April 19, 2011 19:26 — forked from aiwilliams/date_select.js.coffee
CoffeeScript vs. Ruby/Haml vs. Haskell
import Text.Html
dateSelect year = dateSelect "born_on_month" [1..12] monthName
+++ dateSelect "born_on_day" [1..31] show
+++ dateSelect "born_on_year" [year-80..year] show
where monthName x = words "January February March April May June July August September October November December" !! (x - 1)
dateSelect n xs f = (select << map (dateOption f) xs) ! [name n]
dateOption f x = (option << f x) ! [value $ show x]
@austintaylor
austintaylor / 0_map.hs
Created April 20, 2011 15:02
5 ways to do the same thing
products = concatMap (\x -> map (*x) [1..10]) [1..10]
@austintaylor
austintaylor / powerset.hs
Created April 25, 2011 19:16
Powerset: Haskell vs. Ruby
import Control.Monad (filterM)
powerset :: [a] -> [[a]]
powerset = filterM (\x -> [True, False])