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
| //MARK: Strategy protocol | |
| protocol StrategyAny { | |
| func performAny(input: Any) -> Any? | |
| } | |
| extension StrategyAny where Self: Strategy { | |
| func performAny(input: Any) -> Any? { | |
| if let input = input as? Input { | |
| return perform(input: input) |
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 UIKit | |
| class LoadingView: UIView { | |
| var spinnerContainer: UIView! | |
| var spinner: UIActivityIndicatorView! | |
| //MARK: Initializers | |
| override init(frame: CGRect) { |
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 WeakArray<Element: AnyObject> { | |
| fileprivate var backing: [Weakable<Element>] | |
| init(_ array: [Element]) { | |
| backing = array.map { Weakable($0) } | |
| } | |
| mutating func cleanup() { | |
| backing = backing.flatMap { $0.element != nil ? $0 : nil } | |
| } |
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 | |
| class Node { | |
| let x: Int | |
| let y: Int | |
| var isBlocked = false | |
| // WeakArray.swift is in its own gist | |
| var neighbors = WeakArray<Node>() | |
| var unblockedNeighbors: [Node] { | |
| return neighbors.filter { !$0.isBlocked } |
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
| extension Collection { | |
| func filterSplit(predicate: (Iterator.Element) -> Bool) -> (pass: [Iterator.Element], fail: [Iterator.Element]) { | |
| var pass = Array<Iterator.Element>() | |
| var fail = Array<Iterator.Element>() | |
| for element in self { | |
| if predicate(element) { | |
| pass.append(element) | |
| } else { | |
| fail.append(element) | |
| } |
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
| let names = [ | |
| "3 Green Dogs", | |
| "Absorption Corp", | |
| "Addiction", | |
| "ADM Alliance Nutrition", | |
| "B & R Plastics", | |
| "Cat Dancer" | |
| ] | |
| let results = names.reduce([String:[String]]()) { (dictionary, name) in |
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
| class AuthRetryToken { | |
| private(set) var retriesLeft = 3 | |
| var canRetry: Bool { | |
| return retriesLeft > 0 | |
| } | |
| func handleRetry() { | |
| guard canRetry else { return } | |
| retriesLeft -= 1 | |
| } |
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 Optionalable { | |
| associatedtype WrappedType | |
| var wrapped: WrappedType? { get } | |
| } | |
| extension Optional: Optionalable { | |
| typealias WrappedType = Wrapped | |
| var wrapped: Wrapped? { | |
| return 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 Foundation | |
| #if os(Linux) | |
| class XMLBaseClass {} | |
| #else | |
| // On macOS, XMLParserDelegate inherits from NSObjectProtocol | |
| typealias XMLBaseClass = NSObject | |
| #endif | |
| class AdsParser: XMLBaseClass, XMLParserDelegate { |
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 Dispatch | |
| import Foundation | |
| import HTTP | |
| extension Responder { | |
| /// Perform several network requets concurrently | |
| /// | |
| /// - Parameters: | |
| /// - requests: An array of Requests to be performed |