Skip to content

Instantly share code, notes, and snippets.

Inspired from https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch5.html
// Curries by partially applying 1 out of 2 arguments
func curry<A, B, C>(fn: (A, B) -> C, _ val: A) -> (B -> C) {
return { b in
fn(val, b)
}
}
// "compose" operator
@raheelahmad
raheelahmad / Hotel.swift
Created January 29, 2016 04:44
Using RangeReplaceableCollectionType
//: Playground - noun: a place where people can play
struct Room: IntegerLiteralConvertible {
let number: Int
init(integerLiteral value: Int) {
number = value
}
}
@raheelahmad
raheelahmad / gist:3f48bc5fc83a4a611904
Last active August 29, 2015 14:06
Core Data stack in Swift
//
// PersistenceStack.swift
import CoreData
public class PersistenceStack: NSObject {
let mainContext: NSManagedObjectContext = {
let context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
context.undoManager = nil
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
@raheelahmad
raheelahmad / tree.md
Created April 24, 2012 01:27 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@raheelahmad
raheelahmad / Synchronized Methods
Created April 3, 2012 14:27
Implementing synchronized methods in shared data
import java.util.Random;
class Counter {
private int c=0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
@raheelahmad
raheelahmad / gist:2292304
Created April 3, 2012 14:12
Joining another thread
import java.util.Random;
class Runner implements Runnable {
public void run() {
int c = 0;
while (c++ < 10000)
if (c % 100 == 0)
System.out.println("still at " + c);
}
}
import java.util.Random;
public class ThreadingThings implements Runnable{
int myID;
Random gen;
public ThreadingThings(int anID) {
myID = anID;
gen = new Random();
@raheelahmad
raheelahmad / UsingInitialize.m
Created March 13, 2012 22:44
Using initialize to keep track of instances created by the class object
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
@end
@implementation MyClass
static NSMutableArray *instances;
@raheelahmad
raheelahmad / gist:1573407
Created January 7, 2012 01:35
Inversion count
totalInversions = 0
countInversions (array, start, end)
if (start < end)
int mid = (start + end)/2
countInversions(array, start, mid)
countInversion(array, mid+1, end)
merge(array, start, mid, end)
// else do nothing
@raheelahmad
raheelahmad / gist:1494298
Created December 18, 2011 20:01
Intense Core Data migration
- (BOOL) createRelationshipsForDestinationInstance:(NSManagedObject *) destinationAttendance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error {
NSArray *sourceAttendances = [manager sourceInstancesForEntityMappingNamed:[mapping name]
destinationInstances:[NSArray arrayWithObject:destinationAttendance]];
NSManagedObject *sourceAttendance = [sourceAttendances objectAtIndex:0];
NSArray *allStudents = [[[sourceAttendance valueForKey:@"theSchedule"] valueForKey:@"theCourse"] valueForKey:@"students"];
NSArray *presentStudents = [sourceAttendance valueForKey:@"students"];
NSLog(@"For attendance on %@", [sourceAttendance valueForKey:@"date"]);