Skip to content

Instantly share code, notes, and snippets.

View curtclifton's full-sized avatar

Curt Clifton curtclifton

View GitHub Profile
//: Playground - noun: a place where people can play
import UIKit
class VC {
var dataSource: DataSource?
func viewDidLoad() {
let items = dataSource?.numberOfItemsIn(section: 9) ?? -1
print("There are \(items) items")
//
// NetworkIndicatorManager.swift
// Indicator
//
// Created by Curt Clifton on 5/17/16.
// Copyright © 2016 curtclifton.net. All rights reserved.
//
import Foundation
import UIKit
@curtclifton
curtclifton / StateMachine.swift
Created November 8, 2015 05:32
Mutually recursive protocols seem to be beyond the Swift type system currently. Or am I missing something?
//: Mutually recursive protocols?
protocol State {
typealias EventType: Event // error: type may not reference itself as a requirement
mutating func transitionWithEvent(event: EventType)
}
protocol Event {
typealias StateType: State // error: type may not reference itself as a requirement
var action: (StateMachine<StateType, Self>) -> () { get }
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
struct Foo {
private(set) var bar: String
let baz: String
//: Zippers, based on http://learnyouahaskell.com/zippers
import UIKit
enum FileSystemItem {
case File(name: String, data: String)
case Folder(name: String, contents: [FileSystemItem])
var name: String {
switch self {
import Foundation
// Add a typealias for the child type so different implementors can be specific about the types of their children.
protocol NodeRepresentedObject {
typealias Child
var children: [Child]? {get}
}
protocol EmailMessage: NodeRepresentedObject {
}
@curtclifton
curtclifton / gist:6ff5ea9d1169a249456d
Created February 8, 2015 23:10
Hacking on a type-safe JSON library
//
// JSONShape.swift
// TypedJSON
//
// Created by Curt Clifton on 2/7/15.
// Copyright (c) 2015 curtclifton.net. All rights reserved.
//
import Foundation
@curtclifton
curtclifton / gist:9923c50bfc4b0e0920e2
Last active August 29, 2015 14:02
Information hiding in Swift by nesting class declaration inside a function
protocol Counter {
var value: Int { get }
func moveTowardZero()
}
func makeCounterStartingAt(value: Int) -> Counter {
// Since this class declaration is nested in the generator function, the returned value is only accessible via the Counter protocol
class CountImplementation: Counter {
var value: Int