Skip to content

Instantly share code, notes, and snippets.

@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)
}
@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 / 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 / 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 / simpleRuntime
Last active August 29, 2015 14:10
Simple Objective-C runtime examples
@interface ArchivesEasy : NSObject
@end
@implementation ArchivesEasy
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
[self enumeratePropertiesWithBlock:^(NSString *key, id value) {
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
}];
@hirad
hirad / SwiftComprehension
Created May 4, 2015 18:21
Simple List Comprehensions in Swift
infix operator <- { associativity right precedence 140 }
infix operator |? { associativity right precedence 140 }
func |? <S>(source: [S], predicates: [(s: S) -> Bool]) -> [S] {
var ps = predicates
return count(ps) > 0 ? (filter(source, ps.removeLast())) |? ps : source
}
func <-<S, T>(transform: (s: S) -> T, source: [S]) -> [T] {
return map(source, transform)
@hirad
hirad / RPNSolver
Last active April 29, 2017 18:03
A Simple RPN Calculator in Swift
struct Stack<T> {
let values: [T]
init(_ v: [T] = [T]()) {
self.values = v
}
func push(elem: T) -> Stack<T> {
let newValues = [elem] + self.values
return Stack<T>(newValues)
@hirad
hirad / W2D2 In Class
Created July 8, 2015 00:47
LHL W2D2 In Class Example
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSError* error = nil;
NSString* textString = [NSString stringWithContentsOfFile:@"/path/to/some/file.txt"
encoding:NSUTF8StringEncoding
error:&error];
if (textString == nil) {
NSLog(@"Something went wrong with reading the file: %@", error);
}
@hirad
hirad / CoreDataBasics.m
Last active August 29, 2015 14:25
Core Data Basics
// Setting up the stack
NSString* libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString* storePath = [libraryDir stringByAppendingPathComponent:@"store.sqlite"];
NSURL* storeURL = [NSURL fileURLWithPath:storePath];
NSURL* momURL = [[NSBundle mainBundle] URLForResource:@"EmploymentModel" withExtension:@"momd"];
NSManagedObjectModel* mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
NSPersistentStoreCoordinator* psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSDictionary* storeOptions = @{NSMigratePersistentStoresAutomaticallyOption: @YES,
@hirad
hirad / AsyncOperation.swift
Last active October 9, 2015 21:49
A simple NSOperation subclass to make creating concurrent operations easier
public class AsyncOperation: NSOperation {
enum State {
case Pending
case Executing
case Finished
case Cancelled
var keyValueCodingKeys: [String] {
switch self {