Skip to content

Instantly share code, notes, and snippets.

@asross
asross / gist:4445325
Last active December 10, 2015 14:08
A bash function which, for arguments foo, bar, and baz, evaluates $ grep foo | grep bar | grep baz
function cgrep {
local command=""
for var in "$@"; do
command+="grep $var | "
done
eval ${command% | }
}
#!/usr/bin/env ruby
require 'time'
@default_root_dir = '~/code' # CHANGE THIS
def print_git_logs_since(date_str, root_directory)
logs_by_date_by_project = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] =[]}}
root_directory ||= @default_root_dir
@asross
asross / gist:6252889
Last active December 21, 2015 04:58
simple profiling
def time(&block)
$breakpoints = []
start = Time.now
yield
puts "----------------\n"
puts "Total time:"
puts "\t#{(Time.now - start).round(1)}s"
puts "Breakdown:"
$breakpoints.each.with_index do |(msg, t), i|
puts "\t#{(t-start).round(1)}s, breakpoint #{i} #{"(#{msg})" if msg}"
@asross
asross / prisoner_problem_protocol.rb
Created January 25, 2014 23:30
A simulation of two solutions to the "prisoner and two switches" problem (http://www.braingle.com/brainteasers/18012/prisoners-and-2-switches-scenario-a.html)
class Leader
attr_reader :count
attr_reader :actually_visited
def initialize
@count = 1
@actually_visited = false
end
def respond_to_state(s)
function supersay {
say -v princess '$1' &
say -v 'Agnes' $1 &
say -v 'Albert' $1 &
say -v 'Alex' $1 &
say -v 'Bad News' $1 &
say -v 'Bahh' $1 &
say -v 'Bells' $1 &
say -v 'Boing' $1 &
say -v 'Bruce' $1 &
function groc { # git rebase origin/{current}
local remote_branch="${1:-origin}/$(git branch | grep \* | cut -c 3-)"
git rebase $remote_branch
}
function gpoc { # git push origin {current}
local current_branch="$(git branch | grep \* | cut -c 3-)"
git push origin $current_branch
}
def twitter_puddle(wall):
height = max(wall)
total = 0
for x in range(1, len(wall)-2):
lowest_wall = min(max(wall[:x]), max(wall[x+1:]))
if lowest_wall > wall[x]:
total += lowest_wall - wall[x]
return total
assert(twitter_puddle([2, 5, 1, 2, 3, 4, 7, 7, 6]) == 10)
@asross
asross / font_randomizer.js
Last active August 29, 2015 13:58
Randomize the size and family of all fonts.
function randomizeFonts() {
if (window.jQuery) {
elements = jQuery('*');
families = jQuery.unique(elements.map(function() { return jQuery(this).css('font-family') }));
families.push("'comic sans ms', sans-serif");
elements.each(function() {
jQuery(this).css('font-size', 32*Math.random() + 'px');
jQuery(this).css('font-family', families[Math.floor(Math.random()*families.length)]);
});
# execute e.g.
# $ kill_matching phantomjs
function kill_matching {
export matchee=$1
ruby -e 'puts `ps aux | grep #{`echo $matchee`}`.split("\n").each{|l| `kill -9 #{l.split[1]}` unless l.include?("grep") }'
}
class DeepOpenStruct
def initialize(hash)
hash.each { |k, v| define_singleton_method(k) {
instance_variable_get(:"@#{k}") || instance_variable_set(:"@#{k}", coerce(v))
} }
end
def [](k)
send(k)
end