Skip to content

Instantly share code, notes, and snippets.

@PavelGnatyuk
PavelGnatyuk / String+Monoid.Swift
Created November 22, 2020 07:20
String extension for Monoid
extension String: Monoid {
static var neutral: String = ""
func operation(_ other: String) -> String {
self + other
}
}
@PavelGnatyuk
PavelGnatyuk / MonoidProtocol.swift
Created November 22, 2020 07:18
Monoid protocol in Swift
protocol Monoid {
static var neutral: Self { get }
func operation(_ other: Self) -> Self
}
import UIKit
class ViewController: UIViewController {
private lazy var textFieldName: UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.font = UIFont.preferredFont(forTextStyle: .body)
textField.borderStyle = .roundedRect
textField.delegate = self
@PavelGnatyuk
PavelGnatyuk / coordinator.swift
Created March 2, 2018 15:32
Coordinator with two view-controllers
class Coordinator: FirstControllerDelegate {
let window: UIWindow
var navigation: UINavigationController?
lazy var firstViewController: FirstViewController = {
let controller = FirstViewController()
controller.delegate = self
return controller
}()
@PavelGnatyuk
PavelGnatyuk / goodMorning1.swift
Last active July 11, 2018 06:14
autoclosure sample code 1
func goodMorning(morning: Bool, whom: String) {
if morning {
print("Good morning, \(whom)")
}
}
goodMorning(morning: true, whom: "Pavel")
goodMorning(morning: false, whom: "John")
func goodMorning(morning: Bool, whom: String) {
if morning {
print("Good morning, \(whom)")
}
}
func giveAname() -> String {
print("giveAname() is called")
return "Robert"
}
func goodMorning(morning: Bool, whom: @autoclosure () -> String) {
if morning {
print("Good morning, \(whom())")
}
}
func giveAname() -> String {
print("giveAname() is called")
return "Robert"
}
func goodMorning(morning: Bool, whom: () -> String) {
if morning {
print("Good morning, \(whom())")
}
}
func giveAname() -> String {
print("giveAname() is called")
return "Robert"
}
var isDebug = true
func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String) {
guard isDebug else { return }
if condition() {
print(message())
}
}
func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String) {
if condition() {
print(message())
}
}
func conditionOne() -> Bool {
return true
}