Skip to content

Instantly share code, notes, and snippets.

View devonc's full-sized avatar

Devon devonc

  • Apple Inc.
  • Cupertino, CA
View GitHub Profile
@airspeedswift
airspeedswift / match.swift
Last active February 3, 2018 23:23
Minimalist Regex Matcher in Swift
/// Brian Kernighan's article in Beautiful Code talks about the minimalist regex matcher written by
/// Rob Pike for their Practice of Programming book as "one of the best examples of recursion that I
/// have ever seen, and it shows the power of C pointers".
///
/// Swift strings don't use pointers, and the original code relied heavily on the last character of a
/// C string being `\0`, but you can reproduce many of the nice aspects of the original C code using a
/// combination of slicing and `dropFirst`, the `first` function that returns an optional you can then
/// compare to a non-optional character, and the Swift 1.2 `if...let...where`
///
/// In theory no string copying should be happening since the slices are just a subrange view on the
@andymatuschak
andymatuschak / MultiDirectionAdjudicatingScrollView.swift
Created January 26, 2015 19:31
Source for the Khan Academy app's unusual scrolling interactions
//
// MultiDirectionAdjudicatingScrollView.swift
// Khan Academy
//
// Created by Andy Matuschak on 12/16/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
@wouter-swierstra
wouter-swierstra / gist:0b6062c9660e751cd535
Created October 13, 2014 06:54
A type-level SAT solver in Swift
// A type-level SAT solver in Swift
// Two types to model Booleans
struct True { }
struct False { }
// Negation, Conjunction, Disjunction
@natecook1000
natecook1000 / dumpmodule.sh
Created October 8, 2014 21:05
Dump the Swift-synthesized headers for a module
#! /bin/sh
# usage: <shellscript> [--osx] typename
if [ "$1" = "--osx" ] ; then
echo ":print_module $2" | xcrun swift -deprecated-integrated-repl
else
sdk_path=$(echo `xcrun --show-sdk-path` | sed 's#MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk#iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk#')
echo ":print_module $1" | xcrun swift -deprecated-integrated-repl -sdk "$sdk_path"
fi
@hooman
hooman / AssociatedObjects.swift
Last active May 4, 2022 07:33
Take 2.0: A completely new take on Objective-C associated types. (See history for the ancient approach)
// Playground - noun: a place where people can play
//
//
// By: Hooman Mehr (hooman@mac.com)
// Gist: https://gist.github.com/hooman/599e381d5f037b87d20b (The latest version is always here)
//
//
// Update: 07/30/2014
// Added WeaklyOwned & removeOrphans for memory management support, added more generics & basic access control.
// 08/06/2014
@peeblesjs
peeblesjs / gist:9288f79322ed3119ece4
Last active February 11, 2016 23:31
A naive "valueForKey:" operator in Swift
operator infix --> {}
func --> (instance: Any, key: String) -> Any? {
let mirror = reflect(instance)
for index in 0 ..< mirror.count {
let (childKey, childMirror) = mirror[index]
if childKey == key {
return childMirror.value
}
}
// See: https://devforums.apple.com/message/1000934#1000934
import Foundation
// Logic
operator prefix ¬ {}
@prefix func ¬ (value: Bool) -> Bool {
return !value
}
/**
Provides the ability to verify key paths at compile time.
If "keyPath" does not exist, a compile-time error will be generated.
Example:
// Verifies "isFinished" exists on "operation".
NSString *key = SQKeyPath(operation, isFinished);
// Verifies "isFinished" exists on self.
@steipete
steipete / CaseInsensitiveDictionary.m
Created December 1, 2013 19:12
Implemented as a category on NSDictionary.
static Boolean PSPDFCaseInsensitiveEqualCallback(const void *a, const void *b) {
id objA = (__bridge id)a, objB = (__bridge id)b;
Boolean ret = FALSE;
if ([objA isKindOfClass:NSString.class] && [objB isKindOfClass:NSString.class]) {
ret = ([objA compare:objB options:NSCaseInsensitiveSearch|NSLiteralSearch] == NSOrderedSame);
}else {
ret = [objA isEqual:objB];
}
return ret;
}
ACTION
AD_HOC_CODE_SIGNING_ALLOWED
ALTERNATE_GROUP
ALTERNATE_MODE
ALTERNATE_OWNER
ALWAYS_SEARCH_USER_PATHS
ALWAYS_USE_SEPARATE_HEADERMAPS
APPLE_INTERNAL_DEVELOPER_DIR
APPLE_INTERNAL_DIR
APPLE_INTERNAL_DOCUMENTATION_DIR