Skip to content

Instantly share code, notes, and snippets.

@spetschu
spetschu / Trie.rb
Last active August 29, 2015 14:12
Trie in Ruby (prefix tree)
=begin
= NAME
trie - prefix tree for query suggestions
= SYNOPSIS
trie = Trie.new
trie.push('my query term', 'my query term suggestion')
trie.push('my other query term', 'my other query term suggestion')
hits1 = trie.find('my')
pp hits1 # 2 hits
@spetschu
spetschu / gist:9276969
Created February 28, 2014 18:39
Debug macros
#define DEBUG_ON // Set to DEBUG_OFF to toggle
#ifdef DEBUG_ON
#define debug(format, ...) CFShow((__bridge void *)[NSString stringWithFormat:format, ## __VA_ARGS__]);
#else
#define debug(format, ...)
#endif
#define debugRect(rect) debug(@"%s x:%.2f, y:%.2f, h:%.2f, w:%.2f", #rect, rect.origin.x, rect.origin.y, rect.size.height, rect.size.width)
#define debugSize(size) debug(@"%s h:%.2f, w:%.2f", #size, size.height, size.width)
@spetschu
spetschu / gist:9082002
Created February 18, 2014 22:43
UIView with rounded rectangle background
#import "RoundedRect.h"
@implementation RoundedRect
-(id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self != nil) {
[self drawBackground];
}
@spetschu
spetschu / NSArray+ListComprehensions
Last active December 24, 2015 01:09
A few handy ruby/smalltalk-like list comprehensions.
typedef BOOL (^BooleanBlock)(id obj); // Returns true or false given an object
typedef id (^ValueBlock)(id obj); // Returns an object given an object (generally of the same type, may or may not be modified)
typedef id (^AccumulatorBlock)(id acc, id obj); // Returns the accumulator object, likely mutated.
// A few ruby-like list comprehensions.
@implementation NSArray (ListComprehensions)
// Returns the first object for which the block returns true. AKA array.match()
-(id) find:(BooleanBlock)block {
NSUInteger index = [self indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
@spetschu
spetschu / Ruby Hash deep subtract
Created February 7, 2011 21:03
Ruby Hash subtract method extended to work for Array and Hash values within the Hash.
# Examples
# {:foo => [1,2,3,4]} - {:foo => [1,2]}
# results in {:foo => [3,4]}
#
# {:bar => {:baz => [8,9]}, :buz => "deleteme"} - {:bar => 8, :buz => "deleteme"}
# results in {:bar => {:baz => [9]}, :buz => nil}
#
class Hash
def - (h)
self.merge(h) do |k, old, new|
"Full screen mode for MacVim
"=======================
" To run MacVim in this mode from the commandline, put this whole thing into a file focus.vim and type:
"
" > mvim -S focus.vim
"
" Original tweaked from http://www.chendry.org/2009/04/22/writeroom-vim-style.html
set lines=40
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\t:\w\$(parse_git_branch)>"