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
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
}
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"
}
func goodMorning(morning: Bool, whom: String) {
if morning {
print("Good morning, \(whom)")
}
}
func giveAname() -> String {
print("giveAname() is called")
return "Robert"
}
@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")
// In the second view-controller
let downloader = ImageDownloader()
func downloadImages() {
let image = self.downloader.download(image: "avatar")
self.imageView.image = image
//Other code updating all images in the view-controller
}