This file contains 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 | |
/// An abstract class that makes building simple asynchronous operations easy. | |
/// Subclasses must override `main()` to perform any work and call `finish()` | |
/// when they are done. All `NSOperation` work will be handled automatically. | |
/// | |
/// Source/Inspiration: https://stackoverflow.com/a/48104095/116862 and https://gist.github.com/calebd/93fa347397cec5f88233 | |
open class AsyncOperation: Operation { | |
public init(name: String? = nil) { | |
super.init() |
This file contains 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 | |
/// A validation rule for text input. | |
public enum TextValidationRule { | |
/// Any input is valid, including an empty string. | |
case noRestriction | |
/// The input must not be empty. | |
case nonEmpty | |
/// The enitre input must match a regular expression. A matching substring is not enough. | |
case regularExpression(NSRegularExpression) |
This file contains 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 | |
extension Decodable { | |
public static func randomInstance() throws -> Self { | |
let decoder = RandomDecoder() | |
return try Self(from: decoder) | |
} | |
} | |
private class RandomDecoder: Decoder { |
This file contains 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 extension Dictionary { | |
public func mapPairs<T: Hashable, U>(_ transform: ((key: Key, value: Value)) throws -> (key: T, value: U)) rethrows -> [T: U] { | |
return .init(uniqueKeysWithValues: try self.map(transform)) | |
} | |
} | |
let x = ["1": 1, "2": 2, "3": 3] | |
let y = x.mapPairs { (Int($0.key)!, "\($0.value)") } | |
print(x) // ["1": 1, "2": 2, "3": 3] |
This file contains 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 PassThroughView: UIView { | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
let result = super.hitTest(point, with: event) | |
if result == self { return nil } | |
return result | |
} | |
This file contains 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
/// A wrapper for a partial representation of a `T`. | |
/// It can be constructed over time, then later used to | |
/// build a complete `T` | |
@dynamicMemberLookup | |
public struct Partial<T> { | |
enum Error: Swift.Error { | |
case invalid(PartialKeyPath<T>) | |
} | |
// MARK: - Private Properties |
This file contains 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 | |
extension Data { | |
var hexString: String { | |
return self.map({ return String(format: "%02hhx", $0) }).joined() | |
} | |
} |
This file contains 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 | |
// Let's use a Command protocol... | |
protocol Command { | |
associatedtype Action | |
static func modalAlert(title: String, accept: String) -> Self | |
static func request(_ request: URLRequest, available: @escaping (Data?) -> Action) -> Self | |
} |
This file contains 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 | |
enum Either<A,B> { | |
case left(A) | |
case right(B) | |
} | |
// Works only using Swift 4.1 | |
extension Either: Codable where A: Codable, B: Codable { | |
enum CodingKeys: CodingKey { |
This file contains 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
// | |
// main.swift | |
// ExtensibleEffects | |
// | |
// Created by Chris Eidhof on 02.01.18. | |
// Copyright © 2018 objc.io. All rights reserved. | |
// | |
import Foundation |