This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(" ") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Responder chain in Swift using enums | |
| protocol CommandProtocol {} | |
| protocol Responder: class { | |
| var nextResponder: Responder? { get } | |
| func performerForCommand | |
| <Command : CommandProtocol> | |
| (command: Command) -> (() -> ())? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Person { | |
| var firstName, lastName: String | |
| mutating func makeScottishClan() { | |
| lastName = "mc\(lastName)" | |
| } | |
| } | |
| // Person.Mutation gets automatically created (like an enum) | |
| // .firstName(String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Types for currency conversion | |
| // https://www.natashatherobot.com/swift-money-phantom-types/ | |
| import Foundation | |
| struct Money { | |
| enum Currency { | |
| case GBP, EUR, USD | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Fruit { | |
| case apple, pear, orange, plum | |
| } | |
| extension Fruit { | |
| var cents: Int { | |
| return switch self [ | |
| .apple: 70, | |
| .pear: 85, | |
| .orange: 40, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| protocol StageProtocol { | |
| func perform() throws -> Self? | |
| var queue: dispatch_queue_t { get } | |
| func run(completion: (() throws -> Self) -> ()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 />; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import Foundation | |
| protocol OptionalParasite { | |
| typealias WrappedParasite | |
| func toArray() -> [WrappedParasite] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Property Observing | |
| // Swift 1.2 | |
| import Cocoa | |
| import XCPlayground | |
| protocol ObjectListenerType { | |
| func objectDidChange<T: AnyObject>(x: T) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol ValueStorable { | |
| subscript(key: String) -> AnyObject? { get set } | |
| } | |
| internal protocol ValueStorableUpdater { | |
| init?(fromStorable storable: ValueStorable) | |
| func updateStorable(inout storable: ValueStorable) | |
| } |