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 | |
// The protocol use to convert your instance into textual pretty form | |
protocol CustomPrintable: CustomStringConvertible { | |
var emoji: String { get } | |
} | |
// Defaults | |
extension CustomPrintable { | |
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 | |
public enum AppColor { | |
case blue | |
case gray | |
case graySeparator | |
var rgba: [CGFloat] { | |
switch 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
/* Test Cases | |
Input: [1, 2, 3, 4] | |
Output: [1, 2, 3, 5] | |
Input: [1, 2, 9, 9] | |
Output: [1, 3, 0, 0] | |
Input: [9, 9, 9, 9] | |
Output: [1, 0, 0, 0] | |
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 | |
/// Extending error to make it alertable | |
extension Error { | |
/// displays alert from source controller | |
func alert(with controller: UIViewController) { | |
let alertController = UIAlertController(title: "Oops ❗️", message: "\(self)", preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) |
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 YearError: Error { | |
case empty | |
case invalidNumber | |
case invalidBirthYear | |
case invalidYear | |
} | |
extension YearError: CustomStringConvertible { |
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 Suit { | |
case hearts | |
case spades | |
case diamonds | |
case clubs | |
} | |
extension Suit { |
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 | |
func findTwoMax(_ array: [Int]) -> (Int, Int)? { | |
guard array.count > 1 else { | |
return nil | |
} | |
var max = Int.min | |
var secondMax = Int.min | |
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 openBrackets: [Character] = ["(", "{", "["] | |
let closedBrackets: [Character] = [")", "}", "]"] | |
// overloading ~= empowers your pattern mathing in switch | |
func ~=<T: Equatable>(pattern: [T], value: T) -> Bool { | |
return pattern.contains(value) | |
} |
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
final class Notifiable<T> { | |
typealias CompletionHandler = ((T) -> ()) | |
private var propertyChanged: CompletionHandler? = nil | |
var value: T { | |
didSet { | |
guard let observer = propertyChanged else { return } | |
observer(value) |
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
var Operations: [String: (Double, Double) -> Double] = [ | |
"*": { $0 * $1 }, | |
"+": { $0 + $1 }, | |
"-": { $1 - $0 }, | |
"/": { $1 / $0 } | |
] | |
let Operators = ["+", "-", "*", "/"] | |
func postfix(_ expression: String) -> Double { |