Skip to content

Instantly share code, notes, and snippets.

//: 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) -> ())
}
@RoyalIcing
RoyalIcing / 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 />;
@RoyalIcing
RoyalIcing / 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)
}
@RoyalIcing
RoyalIcing / 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)
}
internal enum FileInfoIdentifier: String {
case DisplayNameAndIcon = "displayNameAndIcon"
case DateModified = "dateModified"
var sortDescriptor: NSSortDescriptor {
switch self {
case .DisplayNameAndIcon:
return NSSortDescriptor(key:NSURLLocalizedNameKey, ascending:true)
case .DateModified:
return NSSortDescriptor(key:NSURLContentModificationDateKey, ascending:false)
@RoyalIcing
RoyalIcing / WKUserContentController+bundledScripts
Created April 24, 2015 04:00
Easily add scripts from your bundle to a WKUserContentController. Has optional template replacing, specify a dictionary with `sourceReplacements:`.
// Created by Patrick Smith on 22/04/2015.
// Copyright 2015 Patrick Smith.
// Released under the MIT License http://opensource.org/licenses/MIT
import Cocoa
import WebKit
extension WKUserContentController {
func addBundledUserScript(scriptNameInBundle: String, injectAtStart: Bool = false, injectAtEnd: Bool = false, forMainFrameOnly: Bool = true, sourceReplacements: [String:String]? = nil) {
@RoyalIcing
RoyalIcing / example.playground
Created July 13, 2014 09:03
Using enum for getting/setting value and modification date – Inessential · Swift Structs and valueForKey:
import Swift
import Cocoa
// Properties you need as an enum - problem of key value coding is it allows you to type *anything*, typos compile fine.
enum SyncObjectPropertyName {
case Archived
case Title
}
// Protocol shared both for local and server