Skip to content

Instantly share code, notes, and snippets.

@tLewisII
tLewisII / KeyPathLenses.swift
Last active March 20, 2019 23:17
KeyPath Lenses
import Foundation
precedencegroup LeftApplyPrecedence {
associativity: left
higherThan: AssignmentPrecedence
lowerThan: TernaryPrecedence
}
precedencegroup FunctionCompositionPrecedence {
associativity: right
@tLewisII
tLewisII / SafeDeque
Created September 29, 2016 21:04
Safe dequeuing of collection and table view cells
protocol CellWithIdentifier:class {
associatedtype T = Self
static var resuseIdentifier: String { get }
}
extension UICollectionViewCell:CellWithIdentifier { }
extension UITableViewCell:CellWithIdentifier { }
extension CellWithIdentifier {
static var resuseIdentifier: String { return String(self) }
@tLewisII
tLewisII / gist:b817ce113b87eb523027
Created June 27, 2015 19:35
Test if a string has all the same characters.
extension String {
var isHomogeneous:Bool {
let charSet = Set(self.characters)
return charSet.count == 0 || charSet.count == 1
}
}
@tLewisII
tLewisII / ForcePopover
Created May 2, 2015 17:51
Changes in iOS 8.3 breaks older forced popover code.
// Need both of these, since one `adaptivePresentationStyleForPresentationController:` works on 8.0 to 8.2, and the other,
// `adaptivePresentationStyleForPresentationController:traitCollection:` works on 8.3 plus
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
return UIModalPresentationNone;
}
@tLewisII
tLewisII / prepareForSegue.swift
Created August 17, 2014 00:21
Syntax differences
// this...
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
let group = data?[self.tableView.indexPathForSelectedRow().row]
if let destination = segue.destinationViewController as? ViewController {
destination.title = group?.valueForProperty(ALAssetsGroupPropertyName) as? NSString
destination.data = PhotoManager.lazySendableForGroup(manager) <^> group
} else {
println("Cast failed, proposed class of segue view controller is wrong")
}
@tLewisII
tLewisII / StringAttributes
Created July 28, 2014 18:24
Attributed String builder for Swift
// Playground - noun: a place where people can play
import Cocoa
enum StringAttribute {
case List([StringAttribute])
case Font(NSFont)
case BgColor(NSColor)
case FgColor(NSColor)
@tLewisII
tLewisII / JSONLens.swift
Created July 21, 2014 00:11
JSON Lens traversal in Swift
let userjs: NSData = "{\"name\": \"max\", \"age\": 10, \"tweets\": [\"hello\"], \"attrs\": {\"one\": \"1\", \"more\": {\"stuff\": \"again\"}}}"
.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let val = JSValue.decode(userjs)
let traversal = JSValue.key("attrs") • JSValue.key("more") • JSValue.key("stuff")
let getIt = traversal.get(val)
let setIt = traversal.set(val, JSValue.JSString("bob"))
@tLewisII
tLewisII / Reader.swift
Created July 2, 2014 01:55
Try at a Reader Monad in Swift
//this is a playground
struct Reader<R,A> {
let f:R -> A
init(_ fun:R -> A) {
f = fun
}
static func wrap(val:A) -> Reader<R,A> {
return Reader({_ in val})
@tLewisII
tLewisII / Writer.swift
Created June 25, 2014 03:49
Monoid with Dual and Writer
operator infix <> {
associativity left
}
protocol Monoid {
typealias M
func mempty() -> M
func mappend(a:M) -> M
}
@tLewisII
tLewisII / Writer.swift
Last active December 15, 2015 22:30
Basic Writer [String] A Writer monad
//bind
operator infix >>= {
associativity left
}
struct Writer<A> {
let a:A
let s:String[]
init(_ a:A, _ s:String[]) {