Skip to content

Instantly share code, notes, and snippets.

@hirad
hirad / CustomView
Created October 17, 2014 18:03
Simple example of custom drawing
@implementation LHLPrettyView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGSize totalSize = self.bounds.size;
CGFloat fraction = 0.8;
CGFloat radius = rintf(MIN(totalSize.height * fraction / 2, totalSize.width * fraction / 2)); // Why did we use rintf?
@hirad
hirad / Isolation.m
Last active August 29, 2015 14:05
An empty app to demonstrate race conditions in Objective-C and how to fix it with GCD.
@interface LHLAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSNumber* counter; // will making this 'atomic' solve our problem?
@property (strong, nonatomic) dispatch_queue_t isolationQueue;
@property (strong, nonatomic) NSRecursiveLock* lock;
@end
@hirad
hirad / wordCounter.m
Created August 26, 2014 18:37
A simple command-line program demonstrating race conditions.
//
// main.m
// WordCounter
//
// Created by Hirad Motamed on 2014-08-22.
// Copyright (c) 2014 Pendar Labs. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <pthread.h>
@hirad
hirad / myMergeSort.swift
Created June 6, 2014 01:00
Swift Merge Sort
let numbers = [9,29, 83, 19, 48, 65, 25, 30, 18, 1, 15, 84, 72, 63, 71]
func splitToSublists(list: Int[]) -> (Int[], Int[]) {
assert(list.count > 1, "List size must be greater than 1 before split")
let midIndex = (list.count / 2) as Int
var leftSublist: Int[] = Array(list[0..midIndex])
var rightSublist: Int[] = Array(list[midIndex..(list.count)])
return (leftSublist, rightSublist)
}