Skip to content

Instantly share code, notes, and snippets.

View patrickgwsmith's full-sized avatar

Patrick George Wyndham Smith patrickgwsmith

View GitHub Profile
@patrickgwsmith
patrickgwsmith / 1-example.swift
Created May 27, 2016 13:36
Swift Dynamic Properties
public struct Person {
public var firstName: String
public var middleName: String?
public var lastName: String
public var ageInYears: Int
public var fullName: String {
return [firstName, middleName, lastName].flatMap{ $0 }.joinWithSeparator(" ")
}
}
@patrickgwsmith
patrickgwsmith / responder-chain.swift
Last active February 13, 2018 16:05
Responder chain in Swift using enums
//: Responder chain in Swift using enums
protocol CommandProtocol {}
protocol Responder: class {
var nextResponder: Responder? { get }
func performerForCommand
<Command : CommandProtocol>
(command: Command) -> (() -> ())?
@patrickgwsmith
patrickgwsmith / Mutation.swift
Last active April 22, 2016 08:09
Mutation methods as a type, allowing easy copies to be made
struct Person {
var firstName, lastName: String
mutating func makeScottishClan() {
lastName = "mc\(lastName)"
}
}
// Person.Mutation gets automatically created (like an enum)
// .firstName(String)
//: Types for currency conversion
// https://www.natashatherobot.com/swift-money-phantom-types/
import Foundation
struct Money {
enum Currency {
case GBP, EUR, USD
}
enum Fruit {
case apple, pear, orange, plum
}
extension Fruit {
var cents: Int {
return switch self [
.apple: 70,
.pear: 85,
.orange: 40,
import Foundation
protocol StageProtocol {
func perform() throws -> Self?
var queue: dispatch_queue_t { get }
func run(completion: (() throws -> Self) -> ())
}
@patrickgwsmith
patrickgwsmith / 1-loadable.js
Last active November 19, 2015 12:51
React Loadable Component
import React from 'react';
import Spinner from 'react-spinner';
export default function loadable(hasLoadedTest, Component) {
return (props) => {
if (hasLoadedTest(props)) {
return <Component { ...props } />;
}
else {
return <Spinner />;
@patrickgwsmith
patrickgwsmith / SequenceType+compact.swift
Last active February 1, 2019 16:54
Swift compact() for SequenceType
//: Playground - noun: a place where people can play
import Foundation
protocol OptionalParasite {
typealias WrappedParasite
func toArray() -> [WrappedParasite]
}
//: Property Observing
// Swift 1.2
import Cocoa
import XCPlayground
protocol ObjectListenerType {
func objectDidChange<T: AnyObject>(x: T)
}
@patrickgwsmith
patrickgwsmith / main.swift
Created July 26, 2015 08:17
Parse & JSON API
protocol ValueStorable {
subscript(key: String) -> AnyObject? { get set }
}
internal protocol ValueStorableUpdater {
init?(fromStorable storable: ValueStorable)
func updateStorable(inout storable: ValueStorable)
}