Skip to content

Instantly share code, notes, and snippets.

View jtbandes's full-sized avatar
💜
Working on @foxglove!

Jacob Bandes-Storch jtbandes

💜
Working on @foxglove!
View GitHub Profile
// replace annoying boilerplate with declarative justice using FunctionalKit:
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// =>
// nota bene: category on UITableView for convenient factory method is highly recommended.
id maybeCell = [[aTableView maybe] dequeueReusableCellWithIdentifier:CellIdentifier];
#include <stdio.h>
void addOne(int num);
void addOnePtr(int *num);
int main (int argc, const char * argv[]) {
int i = 3; // an int
printf("i == %d\n", i);
printf("&i == %p\n", &i); // reference/address-of operator &
@jtbandes
jtbandes / gist:2155018
Created March 22, 2012 01:27
Recursively generated graphs
# A nifty class providing recursive graph generation
class Graph
def initialize(*args, &blk)
@edges = []
@block = blk
@nodes = 0
@inited = false
yield self, *args if block_given?
end
def init
@jtbandes
jtbandes / gist:2288183
Created April 3, 2012 00:12
Ask Different iPad contest
require "json"
require "zlib"
require "open-uri"
require "pp"
scores = Hash.new {|h,k| h[k] = [] }
for user in %w[13414 9388 9495 8318 4408 3117 219 292 13 5472 218]
print "Fetching page #{p}..."
response = JSON.parse Zlib::GzipReader.new(open("http://api.stackexchange.com/2.0/users/#{user}/answers?key=U4DMV*8nvpm3EOpvf69Rxw((&page=1&pagesize=35&fromdate=1331856000&todate=1333367940&order=desc&sort=votes&site=apple&filter=!b3Wz0N7mYj4b)3")).read
#import "JTAsynchronousImageView.h"
@interface JTAsynchronousImageView ()
@property (nonatomic, copy) void (^loadingBlock)(JTAsynchronousImageViewLoadCompletionHandler completionHandler);
@property (nonatomic, strong) void (^cancellationBlock)();
@end
@implementation JTAsynchronousImageView {
// Queue for the image that is currently loading.
dispatch_queue_t _currentQueue;
func bytes<T: UnsignedInteger>(val: T) -> GeneratorOf<UInt8> {
var current = val as UInt64
var step = 0
return GeneratorOf<UInt8> {
if step >= sizeof(T) { return nil }
current >>= 8; step++
return UInt8(current & UInt64(UInt8.max))
}
}
func sift<T, S: Sequence where S.GeneratorType.Element == Optional<T>>(xs: S) -> GeneratorOf<T> {
var gen = xs.generate()
return GeneratorOf<T> {
var next: T??
do {
next = gen.next()
if !next { return nil }
} while !(next!)
return next!
}
// The free monoid
protocol Free {
class var ε: Self { get }
func +(lhs: Self, rhs: Self) -> Self
}
extension String: Free {
static var ε: String { return "" }
}
[merge]
conflictstyle = diff3
[alias]
b = branch
ci = commit
co = checkout
cp = cherry-pick
di = diff
ds = diff --stat
f = fetch -pt
// Before realizing that NSFastGenerator exists...
func enumerate(c: NSFastEnumeration) -> SequenceOf<AnyObject> {
return SequenceOf<AnyObject> { () -> GeneratorOf<AnyObject> in
let bufSize = 16
var objects = UnsafeMutablePointer<AnyObject?>.alloc(bufSize)
var state = NSFastEnumerationState()
var currentCount = 0