Skip to content

Instantly share code, notes, and snippets.

View calda's full-sized avatar

Cal Stephens calda

View GitHub Profile
@calda
calda / results.txt
Created August 8, 2021 16:22
Survey of [weak self] unwrapping on GitHub Swift code search
GitHub search for “weak self”: https://github.com/search?l=Swift&p=1&q=%22weak+self%22&type=Code
Page 1
Result 1 guard else return
Result 2 guard else return
Result 3 invalid search result
Result 4 if let
Result 5 if let
Result 6 self?.
Result 7 self?.
Result 8 guard else return
@calda
calda / EvolutionProposalPlayground.swift
Created February 23, 2020 22:58
Swift Nested JSON Decoding example
import Foundation
struct EvolutionProposal: Codable {
var id: String
var title: String
var reviewStartDate: Date
var reviewEndDate: Date
enum CodingKeys: String, CodingKey {
@calda
calda / Map Sorting.md
Created April 10, 2019 14:16
Map Sorting
@calda
calda / DateFormatterHelpers.swift
Created April 2, 2019 21:32
LoggingManager.swift
//
// DateFormatterHelpers.swift
// WindowKit
//
// Created by Cal Stephens on 1/20/19.
// Copyright © 2019 Cal Stephens. All rights reserved.
//
import Foundation
@calda
calda / sort-playground.swift
Created September 18, 2018 19:39
`sort` ergonomics
// MARK: Collection + Sort Imprvements
extension Collection {
// an equivalent set of mutating `sort(by:)` methods would also be made available.
func sorted<T: Comparable>(by value: (Element) throws -> T) rethrows -> [Element] {
return try self.sorted(by: { lhs, rhs in
return (try value(lhs)) < (try value(rhs))
@calda
calda / schwartzian.swift
Created September 18, 2018 19:35
Schwartzian Transform experiment
import QuartzCore
// MARK: Extensions
extension Sequence {
func sorted<T: Comparable>(by value: (Element) -> T) -> [Self.Element] {
return self.sorted(by: { lhs, rhs in
@calda
calda / SortDescriptor.swift
Created July 20, 2018 00:58
SortDescriptors (July 18)
public struct SortDescriptor<Element> {
private let keyPath: PartialKeyPath<Element>
private let comparator: (Any, Any) -> ComparisonResult
// For abitrary `Value` types, the consumer has to provide the entire Comparator
public init<Value>(
_ keyPath: KeyPath<Element, Value>,
using comparator: @escaping (Value, Value) -> ComparisonResult)
{
@calda
calda / SortDescriptor.swift
Created July 20, 2018 00:45
SortDescriptor.playground
import Foundation
// MARK: SortDescriptor
/// Type-erased Sort Descriptor (can store multiple in the same array
/// regardless of the underlying KeyPath
public struct SortDescriptor<Element> {
private let comparator: (Any, Any) -> Bool
@calda
calda / tikz
Created March 14, 2017 03:45
tikz
\begin{tikzpicture}[auto, node distance=2cm, every loop/.style={},
thick,main node/.style={circle,draw,font=\sffamily\large\bfseries},graph node/.style={}]
\node[main node] (1) {$v_1$};
\node[main node] (2) [below left of=1] {$v_2$};
\node[main node] (3) [below right of=1] {$v_3$};
\node[main node] (4) [below left of=3] {$v_4$};
\node[main node] (5) [below left of=4] {$v_5$};
\node[main node] (6) [below right of=4] {$v_6$};
\node[main node] (7) [below right of=5] {$v_7$};
@IBDesignable
class CornerRadiusView : UIView {
@IBInspectable var cornerRadius: CGFloat = -1
override func draw(_ rect: CGRect) {
self.layer.cornerRadius = cornerRadius
}
}