Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / SKValueObject.h
Last active August 29, 2015 14:01
SKValueObject
//
// SKValueObject.h
// TinyType
//
// Created by Soroush Khanlou on 5/15/14.
// Copyright (c) 2014 Soroush Khanlou. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>
@interface NSObject (IfClass)
- (id)ifClass:(Class)aClass;
@end
import UIKit
extension UIColor: IntegerLiteralConvertible {
class func convertFromIntegerLiteral(value: IntegerLiteralType) -> UIColor {
let r = Double((value >> 16) & 0xFF) / 255;
let g = Double((value >> 8) & 0xFF) / 255;
let b = Double((value) & 0xFF) / 255;
return UIColor(red:r, green:g, blue:b, alpha:1.0)
}
@interface MappedArray ()
@property (nonatomic) NSArray *backingArray;
@end
@implementation MappedArray
- (instancetype)initWithArray:(NSArray *)array transformationBlock:(id (^)(id object))block {
self = [super init];
@khanlou
khanlou / gist:6091415
Created July 26, 2013 19:06
Blocks are created on the stack frame that they are on
void (^block)();
if (condition) {
block = ^{
NSLog(@"some code");
};
} else {
block = ^{
NSLog(@"other code");
};
}
@khanlou
khanlou / download_all_cloud_app_files.py
Created September 2, 2013 14:26
This is a simple python script for downloading all of the files in your cloud app. Caveats: "bookmarks" can't be downloaded, so they'll be outputted in the log. All files will be downloaded into the same directory as the python script. It requires pycloudapp, which you can find here: https://github.com/originell/pycloudapp.
# requires https://github.com/originell/pycloudapp
from cloudapp.cloud import Cloud
import urllib
mycloud = Cloud()
mycloud.auth("username", "password")
@khanlou
khanlou / gist:7156174
Created October 25, 2013 15:07
Scottish Wizard Voodoo Drink
func doThing(defaultable value: Int = 4, _ required: Void -> Int) -> Int {
return value + required()
}
doThing(defaultable: 4, { 6 }) // works
doThing { 6 } // works
doThing({ 6 }) // doesn't compile: Missing argument for parameter #2 in call
@khanlou
khanlou / Times.swift
Created November 6, 2016 20:52
Ruby's n.times in Swift
extension SignedInteger {
func times() -> AnySequence<()> {
return AnySequence<()>({ () -> AnyIterator<()> in
var count: Self = 0
return AnyIterator<()>({
if count == self {
return nil
}
import Foundation
extension Data {
var hexString1: String {
return self.map({ return String(format: "%02hhx", $0) }).joined()
}
var hexString2: String {
return self.reduce("", { return $0 + String(format: "%02hhx", $1) })