Skip to content

Instantly share code, notes, and snippets.

@brianstorti
brianstorti / rack_middleware.rb
Last active August 29, 2015 14:08
Example rack middlewares
class Canadianize
def initialize(app, arg = "")
@app = app
@arg = arg
end
def call(env)
status, headers, content = @app.call(env)
content[0] += @arg + ", eh?"
[ status, headers, content ]
@brianstorti
brianstorti / gist:6137a1b65bde39562de8
Created July 16, 2014 19:23
Convert git diff to svn patch
git diff --ignore-space-at-eol --no-prefix | sed -e "s/^diff --git [^[:space:]]*/Index:/" -e "s/^index.*/===================================================================/" > patch.diff
@brianstorti
brianstorti / gist:9629116
Created March 18, 2014 20:42
keybase.md
### Keybase proof
I hereby claim:
* I am brianstorti on github.
* I am brianstorti (https://keybase.io/brianstorti) on keybase.
* I have a public key whose fingerprint is AC83 2A13 C81A FD98 4C06 4FF4 01CF 37BD BDBE 035C
To claim this, I am signing this object:
@brianstorti
brianstorti / require_use.clj
Created June 8, 2013 17:13
Clojure `require` and `use`
; with "require", I need to use the namespace-qualified name
(require 'examples.foo)
("bar" examples.foo/function-name)
; I can use "refer" to avoid that
(require 'examples.foo)
(refer 'examples.foo)
("bar" function-name)
; or I can use "use", that will do both the steps for me
; sum the square of the parameters.
(def add-squares
(fn [& args]
(apply + (map * args args))))
(add-squares 1 2 5)
; factorial without iteration/recursion.
(def factorial
(fn [number]
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
}
function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || hostname -s
}
git_prompt_info () {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
@brianstorti
brianstorti / gist:5369011
Last active December 16, 2015 03:19
Breath-first search

Breath-first search (BFS)

Breadth-first search (BFS) is a strategy for searching in a graph.

The BFS begins at a root node and inspects all the neighboring nodes. Then for each of those neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on.

This image shows the order in which the nodes are expanded: image

class Node
attr_reader :name
def initialize(name)
@name = name
@descendants = []
end
def add_edge(descendant, weight)
@descendants << {:node => descendant, :weight => weight}
@brianstorti
brianstorti / gist:5016559
Created February 22, 2013 21:09
use vim as man-page viewer
man() {
PAGER="/bin/sh -c \"col -b | vim -R -c 'set ft=man' -\"" command man $@
}
@brianstorti
brianstorti / gist:5006484
Created February 21, 2013 17:26
Remove capistrano releases
cap <stage> deploy:cleanup -s keep_releases=1