Skip to content

Instantly share code, notes, and snippets.

View knaveofdiamonds's full-sized avatar

Roland Swingler knaveofdiamonds

View GitHub Profile
# with this function defined...
__git_branch ()
{
local b="$(git symbolic-ref HEAD 2>/dev/null)"
if [ -n "$b" ]; then
printf "%s" "${b##refs/heads/}"
fi
}
# this gives you warnings for files changed in your branch
require 'rspec/autorun'
def letters(a, b)
indexes = b.each_char.map do |letter|
a.each_char.each_with_index.select {|l, _| l == letter }.map {|_, i| i }
end
start = indexes.shift
if indexes.empty?
@knaveofdiamonds
knaveofdiamonds / captain.txt
Created November 27, 2012 14:51
Puerto Rico Captain's Phase
* There are n ships, with i,j,k... spaces for barrels of goods.
* There are m players, with x,y,z... number of barrels of goods of different types.
* The goods are: indigo, corn, tobacco, sugar, coffee.
* A ship can only be loaded if there is space on the ship - 1 space
for 1 barrel.
* Each ship can only have one type of goods on it.
@knaveofdiamonds
knaveofdiamonds / snakes.rb
Created October 31, 2012 14:56
Snakes and ladders Kata
def board(size, tunnels)
Hash.new {|_,i| i }.
merge(Hash[(1..6).map{|i| [size + i, size - i] }]).
merge(tunnels)
end
def move(board, players, player, roll)
players[player] = board[ players[player] + roll ]
[players, next_player(players.size, player, roll)]
end
@knaveofdiamonds
knaveofdiamonds / monads.rb
Created October 26, 2012 16:19
Monads in ruby
module Monad
# (a -> b) -> (a -> [b])
def lift(f)
lambda {|x| unit.call(f.call(x)) }
end
# (a -> b) -> (M a -> M b)
def liftM(f)
bind(lift(f))
end
class UsersController < ApplicationController
def create
respond_to do |format|
format.html do
@user = UserService.create(params[:user], true)
redirect_to @user
end
format.json do
@user = UserService.create(params[:user], false)
render :json => @user,
@knaveofdiamonds
knaveofdiamonds / gist:1502171
Created December 20, 2011 16:28
Self-schizophrenia example
# Self-schizophrenia example
class A
def foo
"foo"
end
def bar
"bar"
end
def next_working_day(count = 1)
negative = count < 0
count = count.abs
date = negative ? yesterday : tomorrow
loop do
count -= 1 if date.working_day?
return date if count.zero?
date += (negative ? -1 : 1).day
> module Main where
>
> import Data.List (sort)
I don't need to know anything about the precise positions of the Wire's
end points, all that is important is the relative positions (the problem
states that only 2 wires cross at one point, so I don't have to worry about
the angles of the wires).
Given the wire that is lowest on the left building, I know that another wire
enable :sessions
get "/foo" do
session[:something] = "hello"
"set"
end
get "/bar" do
session[:something]
end