Skip to content

Instantly share code, notes, and snippets.

@cprovatas
cprovatas / TypeReflectable.swift
Last active October 20, 2018 22:28
Access current type by calling Self instead of manually calling <current type>.self
protocol TypeReflectable {
var `Self`: Self.Type { get }
static var `Self`: Self.Type { get }
}
extension TypeReflectable {
var `Self`: Self.Type {
return type(of: self)
}
@cprovatas
cprovatas / interpolation.swift
Created June 8, 2019 22:56
Decorating Command Line Output With DefaultStringInterpolation
enum ASCIIColor: String {
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case green = "\u{001B}[0;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
case `default` = "\u{001B}[0;0m"
@cprovatas
cprovatas / SequenceHelpers.swift
Last active October 12, 2019 07:22
Swift Sequence Helpers
// These helpers allow for basic operations to be performed while still iterating over a sequence once without
// adding all of the common boilerplate code you'd normally have to write
extension Sequence {
// filter + forEach
func forEach(where predicate: (Element) -> Bool, _ body: (Element) throws -> Void) rethrows {
for element in self where predicate(element) {
try body(element)
}
}
@cprovatas
cprovatas / Array+Spread.swift
Last active July 22, 2023 02:04
Spread Syntax for Swift Arrays (ala es6)
extension Array {
static func ... (lhs: [Self.Element], rhs: [Self.Element]) -> [Self.Element] {
var copy = lhs
copy.append(contentsOf: rhs)
return copy
}
static func ... (lhs: Self.Element, rhs: [Self.Element]) -> [Self.Element] {
var copy = [lhs]
copy.append(contentsOf: rhs)