Skip to content

Instantly share code, notes, and snippets.

@wildthink
wildthink / NSBlockInit.swift
Last active March 28, 2016 17:28
NSBlockInit offers a Swift extension to the initialization protocols so you can easily chain a setup block as part of the basic init() are by using setup(Self) chained onto the init.
//
// NSBlockInit.swift
// XjSwiftLab
//
// Created by Jason Jobe on 3/19/16.
// Copyright (c) 2016 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/48cce4cd2f27695ebb08
//
// Thanks to Mike Ash for his expertise and support of the community
// https://www.mikeash.com/pyblog/friday-qa-2015-12-25-swifty-targetaction.html
@wildthink
wildthink / Relay.swift
Last active March 29, 2016 21:12
Relay offers a Swift, type safe way to find a conforming target along the Responder chain and invoke any protocol method on it.
//
// Relay.swift
// XjSwiftLab
//
// Created by Jason Jobe on 5/11/15.
// Copyright (c) 2015 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/19d43871964821c4f293
//
import Foundation
@wildthink
wildthink / transducers.swift
Created November 21, 2016 18:21 — forked from rjchatfield/transducers.swift
Transducers & Reducers in Swift 2
//-- This is an introduction to Transducers in Swift, inspired by
// Talk: https://www.youtube.com/watch?v=estNbh2TF3E
// Code: https://github.com/mbrandonw/learn-transducers-playground
// Updated with Swift 2
/**
* Define a few test methods
*/
/// REDUCER
func append <A> (xs: [A], x: A) -> [A] { return xs + [x] }
@wildthink
wildthink / StringLiteral.swift
Created December 30, 2016 19:23 — forked from russbishop/StringLiteral.swift
Make ExpressibleByStringLiteral tolerable
// If you want types initializable from String literals
// but don't want to implement three separate initializers.
extension ExpressibleByUnicodeScalarLiteral where Self: ExpressibleByStringLiteral, Self.StringLiteralType == String {
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
}
I am jason_ at onename.com
@wildthink
wildthink / dawg.py
Created January 10, 2017 19:37 — forked from smhanov/dawg.py
Use a DAWG as a map
#!/usr/bin/python3
# By Steve Hanov, 2011. Released to the public domain.
# Updated 2014 to use DAWG as a mapping.
import sys
import time
DICTIONARY = "/usr/share/dict/words"
QUERY = sys.argv[1:]
# This class represents a node in the directed acyclic word graph (DAWG). It
@wildthink
wildthink / Domain.swift
Last active January 13, 2017 17:46
Simple data store API for Swift applications.
//
// Domain.swift
// Inspired by Eve
//
// Created by Jason Jobe on 11/26/16.
// Copyright © 2016 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/b911a5b8e0d7dc6ef190fd8fe4f7a2ee
//
import Foundation
import Foundation
extension String {
/**
Returns a country flag from an **uppercased** ISO Alpha-2 country code String
Example usage: "US".flag
@return A flag emoji. Example: 🇺🇸
*/
@wildthink
wildthink / CGRectAspectFit.m
Created March 22, 2017 17:15 — forked from lanephillips/CGRectAspectFit.m
Objective-C code to fit a CGRect inside or outside another CGRect while maintaining aspect ratio. The fitted rectangle is centered on the target rectangle.
CGFloat ScaleToAspectFitRectInRect(CGRect rfit, CGRect rtarget)
{
// first try to match width
CGFloat s = CGRectGetWidth(rtarget) / CGRectGetWidth(rfit);
// if we scale the height to make the widths equal, does it still fit?
if (CGRectGetHeight(rfit) * s <= CGRectGetHeight(rtarget)) {
return s;
}
// no, match height instead
return CGRectGetHeight(rtarget) / CGRectGetHeight(rfit);
@wildthink
wildthink / DocumentViewControllerExtension.swift
Created April 2, 2017 14:58
NSDocument ViewController Hooks
extension NSViewController {
var document: AnyObject? {
if let target = self.parent, target.responds (to: #selector(getter: self.document)) {
return target.perform(#selector(getter: self.document)) as AnyObject?
}
return self.view.window?.windowController?.document
}
var managedObjectContext: NSManagedObjectContext? {