Skip to content

Instantly share code, notes, and snippets.

@rayfix
rayfix / InitRestKit.m
Created August 5, 2011 07:23
First mapping
- (void)mapObjects:(RKObjectManager*)objectManager
{
RKObjectMapping* widgetMapping = [RKObjectMapping mappingForClass:[widget class]];
[widgetMapping mapKeyPath:@"id" toAttribute:@"widgetID"];
[widgetMapping mapAttributes:@"name", @"description", nil];
[widgetMapping mapKeyPath:@"public" toAttribute:@"isPublic"];
[widgetMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[widgetMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];
[objectManager.mappingProvider setMapping:widgetMapping forKeyPath:@"widget"];
}
@rayfix
rayfix / gist:1135157
Created August 9, 2011 20:51
mapping problem ... crud operations
RKObjectMapping* channelMapping = [RKObjectMapping mappingForClass:[Channel class]];
// Core data
//channelMapping.primaryKeyAttribute = "channelID";
// For now using NSObject derived thing
[channelMapping mapKeyPathsToAttributes:
@"id", @"channelID",
@"created_at", @"createdAt",
@"updated_at", @"updatedAt",
@rayfix
rayfix / gist:1150539
Created August 17, 2011 00:41
What is the best format for RestKit?
RestKit allows a great deal of customization and can probably be made to handle most formats.
Since I have full control over the server (now), I want to put the onus on the server as much
as possible and reduce unnecessary bandwidth. However, I want to keep the RestKit side code
as easy, and concise as possible.
So suppose I have a set of widget classes with ids and names with standard CRUD operations.
I have an index action which could potentially return a large number of widgets and so need
to be paginated. Here is one way I thought of.
Create Action
@rayfix
rayfix / merge_sort.cpp
Last active December 19, 2015 08:59
Simple merge sort implementation in C++. A little more polish. Thanks to feedback from @matt_dz
#include <iostream>
#include <vector>
#include <functional>
namespace rf {
template <typename T, typename Compare = std::less<T>>
std::vector<T> merge_sort(const std::vector<T>& input,
const Compare& compare = Compare())
{
@rayfix
rayfix / merge_sort.rb
Last active December 19, 2015 09:09
Naive merge sort in ruby.
class Array
def merge_sort
c = self.count
return self if c <= 1
mid = c/2
left = self[0,mid].merge_sort
right = self[mid, mid+1].merge_sort
output = []
l = 0;
@rayfix
rayfix / RFViewController.m
Created August 17, 2013 23:08
Our animations were not working with our dispatch queue. I boiled it down to a simple test case. However, the test case works perfectly as expected so there is another problem that is happening. This code makes a view oscillate back and forth.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self move];
}
- (void)move
{
static BOOL flipFlop;
@rayfix
rayfix / GroupTests.m
Created March 16, 2014 00:29
Groups from NSArray
//
// GroupTests.m
//
// Created by Ray Fix on 3/15/14.
//
#import <XCTest/XCTest.h>
#import "NSArray+Groups.h"
@interface GroupTests : XCTestCase
@rayfix
rayfix / gist:02ca4d9537af011dd12f
Created June 22, 2014 07:56
Compute Probability of Ace of Diamonds in a Poker Hand
func testPokerAceOfDiamonds()
{
let handsWithAceOfDiamonds = Binomial(n: 51, choose: 4)
let allPossibleHands = Binomial(n: 52, choose: 5)
let probabilityOfAceOfDiamonds = handsWithAceOfDiamonds / allPossibleHands
XCTAssertEqual(probabilityOfAceOfDiamonds, Rational(5,52))
let probabilityOfNoAceOfDiamonds = Rational(1) - probabilityOfAceOfDiamonds
@rayfix
rayfix / gist:1fb467981e386ad5797e
Created August 31, 2014 14:56
Swift Ring Buffer
//
// Ring.swift
//
// Copyright (c) 2014 Pelfunc, Inc. All rights reserved.
//
/// Allow iteration of Ring<T> in LIFO order
public struct RingGenerator<T> : GeneratorType {
@rayfix
rayfix / didSet_on_array.swift
Created April 7, 2015 18:53
didSet on array crashes my playground
// Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
struct Thing {
var arr: [Int] = [] {
didSet {