Skip to content

Instantly share code, notes, and snippets.

View pilky's full-sized avatar

Martin Pilkington pilky

View GitHub Profile
@pilky
pilky / URLSessionProtocol.swift
Created February 28, 2017 10:45
Swift compiler oddity
protocol URLSessionProtocol {
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
protocol URLSessionDataTaskProtocol {
func resume()
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
@pilky
pilky / KeyValueMap.swift
Created February 17, 2017 17:24
Key value mapping for swift dictionaries
import UIKit
extension Dictionary {
init<S: Sequence>(_ pairs: S) where S.Iterator.Element == (Key, Value) {
self = [:]
for (k,v) in pairs { self[k] = v }
}
func keyValueMap<T: Hashable, S>(_ transform: (Key, Value) throws -> (T, S)) rethrows -> [T: S] {
var newDict = [T: S]()
@interface MyObject : NSObject
@property (copy) NSString *myProperty;
@end
@interface MyObject (Category)
- (NSNumber *)myProperty;
@pilky
pilky / gist:d0893c6a146220a43db5090e520b8f18
Last active March 29, 2016 22:04
Swift argument swapping weirdness
func f(a a: Int = 0, b: Int = 0, c: Int, d: Int = 0, e: Int = 0) -> (Int, Int, Int, Int, Int){
return (a, b, c, d, e)
}
f(c:3)
f(a:1, c:3)
f(b:2, c:3)
f(a:1, b:2, c:3)
f(b:2, a:1, c:3)
f(b:2, c:3, a:1) //Bad
@pilky
pilky / gist:e6274392313f1b8f724e
Created March 24, 2016 13:29
Automatically filing a train ticket in "Receipts/2015-16/September"
1. Need to recognise it's a train ticket
2. Need to recognise it's a train ticket to Aberystwyth
3. Need to recognise the date on the ticket
4. Need to know that the destination and date means it was for travelling to a conference
5. Needs to know that I run a business, and conference travel is a business expense, so needs to be classed as a receipt
6. Needs to know I want to view receipts by year and month
7. Needs to know by "year" I mean my company accounting year, not calendar year
8. Needs to know my company accounting year
@pilky
pilky / main.m
Created January 20, 2016 15:49
Compiler weirdness
#import <AppKit/AppKit.h>
/**
* This seems to work fine, if repeat count is 0 then repeatCount is set to another values. However it doesn't seem to match HUGE_VALF
*/
void basicRepeatCount()
{
NSInteger repeatCount = 0;
if (repeatCount == 0) {
repeatCount = HUGE_VALF;
@pilky
pilky / gist:e769b72c2f2ad0b21623
Last active August 29, 2015 14:24
Letter to MP on Encryption
I was rather disheartened to read last night that David Cameron's ridiculous comments from before the election wanting to ban encryption may actually be put forward in a bill (http://uk.businessinsider.com/david-cameron-encryption-back-doors-iphone-whatsapp-2015-7).
I wanted to write to you to urge you to oppose this if the government do attempt to make it law. Strong encryption is the foundation of our economy, our right to privacy and our personal and national security in a digital world. Encryption is key to ensuring that the data we store on computers or send between them is only seen by the right people.
There are two proposals that have been floated, both of which are technically flawed (if not impossible) and could have devastating effects on our country. The first is to provide backdoors in encryption for law enforcement and security agencies, the second is to ban encryption outright. The first option is technically impossible, as any backdoor is then available to everyone and so has the same effect
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="13F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6254"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner"/>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe">
@pilky
pilky / gist:5593296
Last active December 17, 2015 10:18
#include <stdbool.h>
struct rxt_enumerator_state {
rxt_node *node;
bool leftFinished;
bool rightFinished;
bool returned;
rxt_enumerator_state *previousState;
}
//We use a typedef to make things simpler
typedef void (*rxt_enumerator)(char *key, char *value);
void rxt_enumerate_node(rxt_node *root, rxt_enumerator callback) {
if (!root) return;
if (root->color) {
if (root->color == 2) root = root->value;
//All we change is that instead of printing out, we now call the function supplied to us
if (callback) callback(root->key, root->value);