Skip to content

Instantly share code, notes, and snippets.

View mdippery's full-sized avatar
💭
Have you seen an older me go by in a time machine?

Michael Dippery mdippery

💭
Have you seen an older me go by in a time machine?
View GitHub Profile
@mdippery
mdippery / sort-string.m
Created March 31, 2011 17:21
String sorting example in Objective-C
#import <Foundation/Foundation.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int compare_char(const char *a, const char *b)
{
if (*a > *b) {
return 1;
} else if (*a < *b) {
@mdippery
mdippery / template_cache.py
Created November 11, 2011 20:06
Creates a template cache key for Django
from django.utils.hashcompat import md5_constructor
from django.utils.http import urlquote
def template_cache_key(fragment_name, *vary_on):
"""Builds a cache key for a template fragment.
This is shamelessly stolen from Django core.
"""
base_cache_key = "template.cache.%s" % fragment_name
args = md5_constructor(u":".join([urlquote(var) for var in vary_on]))
@mdippery
mdippery / join.m
Created November 30, 2011 21:27
Python's os.path.join, for Objective-C
#import <Foundation/Foundation.h>
#import <stdarg.h>
@interface NSString (Paths)
+ (NSString *)stringByJoiningPathComponents:(NSString *)part, ...;
@end
@implementation NSString (Paths)
+ (NSString *)stringByJoiningPathComponents:(NSString *)part, ...
{
@mdippery
mdippery / slice.m
Created November 30, 2011 22:21
Ruby's string slicing functions, now in delicious Objective-C flavor!
#import <Foundation/Foundation.h>
@interface NSString (Slicing)
- (unichar)characterAt:(NSInteger)index;
- (NSString *)stringBySlicingFromIndex:(NSInteger)start length:(NSUInteger)length;
- (NSString *)stringBySlicingFromIndex:(NSInteger)start toIndex:(NSInteger)end;
@end
@implementation NSString (Slicing)
- (unichar)characterAt:(NSInteger)index
@mdippery
mdippery / gist:1674216
Created January 25, 2012 02:21
Switch between multiple Heroku accounts with ease
#!/usr/bin/env ruby
require 'fileutils'
def die(code=1)
$stderr.puts 'Usage: herokusu [--list | --switch user]'
exit code
end
HEROKU_PATH = File.expand_path '~/.heroku'
@mdippery
mdippery / bmi.hs
Created August 15, 2012 19:15
A BMI calculator in Haskell
{-
N.B.: This is just a simple example of a Haskell program. Don't
take BMI seriously. You're fine just the way you are!
-}
import Data.Char (isDigit)
import Data.List (isInfixOf, isSuffixOf)
import Data.List.Split (splitOn)
import System.Console.GetOpt (ArgOrder(..), getOpt)
import System.Environment (getArgs, getProgName)
@mdippery
mdippery / BToA.hs
Created August 28, 2012 01:43
Converts a binary representation of a character into a printable character
import Data.Bits (shift)
import Data.Char (chr)
strip = takeWhile (\ch -> ch /= '\n')
removeSpaces = filter (\ch -> ch == '0' || ch == '1')
chrToInt '0' = 0
chrToInt '1' = 1
@mdippery
mdippery / objcprops.m
Created November 19, 2012 22:03
List declared properties for an Objective-C class
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface NSObject (DeclaredProperties)
- (NSArray *)properties;
@end
@interface TestClass : NSObject
@property (readonly) NSString *prop1;
@mdippery
mdippery / FizzBuzz.hs
Last active December 16, 2015 21:20
FizzBuzz in Haskell
fizzBuzz n
| n `rem` 5 == 0 && n `rem` 3 == 0 = "FizzBuzz"
| n `rem` 5 == 0 = "Buzz"
| n `rem` 3 == 0 = "Fizz"
| otherwise = show n
main = putStr $ unlines $ map fizzBuzz [1..100]
def ensure_subscription(fn):
@wraps(fn)
def _ensure_subscription(user, other, trade):
unsubscribe_code = user.profile.get_unsubscribe_code('trade-notifications')
if unsubscribe_code is None:
return
fn_globals = {}
fn_globals.update(globals())
fn_globals['unsubscribe_code'] = unsubscribe_code
call_fn = FunctionType(fn.func_code, fn_globals)