Skip to content

Instantly share code, notes, and snippets.

View zakbarlow1995's full-sized avatar

Zak Barlow zakbarlow1995

  • UK
View GitHub Profile
@zakbarlow1995
zakbarlow1995 / FunctionComposition.swift
Created October 11, 2021 15:04
Compose Functions - Returns a function that applies g to the result of f
precedencegroup CompositionPrecedence {
associativity: left
}
infix operator >>>: CompositionPrecedence
func >>><T, U, V>(
f: @escaping (T) -> U,
g: @escaping (U) -> V
) -> (T) -> V {
@zakbarlow1995
zakbarlow1995 / GetIndexPathArrayBetween.swift
Created April 27, 2020 09:36
Swift - Get IndexPath array between startIndex & endIndex helper function
func getIndexPathArrayBetween(_ startIndex: Int, and endIndex: Int, section: Int = 0) -> [IndexPath] {
assert(startIndex >= 0 && endIndex > startIndex, "startIndex >= 0 && endIndex > startIndex")
return (startIndex...endIndex).map { IndexPath(row: $0, section: section) }
}
@zakbarlow1995
zakbarlow1995 / IsEmptyOrNil.swift
Created March 24, 2020 15:14
Swift Collections - add isEmptyOrNil/isNilOrEmpty: Bool & nilIfEmpty: Wrapped? properties
import Foundation
extension Optional where Wrapped: Collection {
var isEmptyOrNil: Bool {
self?.isEmpty ?? true
}
var isNilOrEmpty: Bool {
isEmptyOrNil
}
@zakbarlow1995
zakbarlow1995 / CurrencySymbols.swift
Last active December 29, 2019 23:38
String array containing unicode currency symbols (Swift)
let currencySymbols = [
"د.إ",
"؋",
"L",
"֏",
"ƒ",
"Kz",
"$",
"ƒ",
"₼",
@zakbarlow1995
zakbarlow1995 / Optional-String-Nil-Coalescing-Operator.swift
Last active September 5, 2019 16:06
Handy quality of life infix operator for string interpolation of optionals
infix operator ???: NilCoalescingPrecedence
public func ??? <T>(optional: T?, defaultValue: @autoclosure () -> String) -> String {
return optional.map { String(describing: $0) } ?? defaultValue()
}
import Foundation
class Debouncer {
/**
Create a new Debouncer instance with the provided time interval.
- parameter timeInterval: The time interval of the debounce window.
*/
init(timeInterval: TimeInterval) {
@zakbarlow1995
zakbarlow1995 / Sweepstakes.swift
Created August 27, 2019 09:20
Rugby World Cup 2019 Sweepstakes
import Foundation
class Person {
let name: String
private var teams = [Team]()
public var sortedTeams: [Team] {
return teams.sorted(by: { $0.rank < $1.rank })
}
@zakbarlow1995
zakbarlow1995 / UITableView+areEmptyRowsHidden.swift
Last active August 6, 2019 14:23
Added an areEmptyRowsHidden property to UITableView: if set to true, hides the empty cells at the bottom of a table view by adding a view with a height of zero as a footer.
import UIKit
extension UITableView {
@IBInspectable var areEmptyRowsHidden: Bool {
set {
tableFooterView = newValue ? UIView(frame: .zero) : nil
}
get {
return tableFooterView != nil
}
@zakbarlow1995
zakbarlow1995 / AddSubviews.swift
Last active July 24, 2019 09:30
UIView extension to add views variadically to the view-hierarchy
import UIKit
extension UIView {
func addSubviews(_ views: UIView...) {
views.forEach { addSubview($0) }
}
}
@zakbarlow1995
zakbarlow1995 / SubviewsRecursive.swift
Created July 24, 2019 09:18
UIView extension to retrieve all subviews recursively
import UIKit
extension UIView {
var subviewsRecursive: [UIView] {
return subviews + subviews.flatMap { $0.subviewsRecursive }
}
}