Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Lockwood nicklockwood

View GitHub Profile
@nicklockwood
nicklockwood / DictBench.swift
Created Jul 27, 2021
A quick test to compare performance of different dictionary lookups in Swift
View DictBench.swift
// A quick test to compare performance of different dictionary lookups in Swift.
// Note: Results recorded on a 2020 M1 MBP. be sure to run with "Optimize for Speed [-O]
import Foundation
let iterations = 1000000
var dictionary = [Int: Int]()
for i in 0 ..< iterations {
dictionary[i] = Int.random(in: 0 ..< 1000)
@nicklockwood
nicklockwood / CodableVersioning.swift
Last active Oct 15, 2021
Example demonstrating how to use versioning for Codable structs
View CodableVersioning.swift
// This gist demonstrates how you can implement versioning for a Codable struct to support loading
// old serialized data after changing the structure. Notable features of this solution:
//
// * No need to make new properties optional, or to perform post-processing on the struct after
// loading in ordeer to populate missing values
// * No need to change the call site - from the outside this struct behaves just the same
// as if we had implemented codable directly in the normal way.
// * Versioning can be applied individually to parents or leaves in a larger tree of
// structs without affecting the other elements
// * This approach will work even if the original struct was not designed with versioning in mind
View Data+Hex.swift
extension Data {
init?(hexString: String) {
let count = hexString.count / 2
var data = Data(capacity: count)
var i = hexString.startIndex
for _ in 0 ..< count {
let j = hexString.index(after: i)
if var byte = UInt8(hexString[i ... j], radix: 16) {
data.append(&byte, count: 1)
} else {
@nicklockwood
nicklockwood / Color.swift
Created Jan 19, 2021
Getting the Display P3 values back out of a UIColor created with init(displayP3Red:green:blue:alpha:)
View Color.swift
import UIKit
// Create color using P3 color space
let linearColor = UIColor(displayP3Red: 1, green: 0.5, blue: 0.2, alpha: 1)
do {
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
linearColor.getRed(&r, green: &g, blue: &b, alpha: &a)
print(r, g, b, a) // 1.07, 0.46, 0.0, 1.0 - not the expected values
}
@nicklockwood
nicklockwood / JaroWinkler.swift
Created Dec 21, 2020
Swift code to calculate the Jaro-Winkler edit distance between two strings
View JaroWinkler.swift
/// The Jaro-Winkler edit distance between two strings (0 - 1)
func editDistance(_ lhs: String, _ rhs: String) -> Double {
return 1 - jaroWinklerSimilarity(Array(lhs), Array(rhs))
}
/// Jaro-Winkler similarity between two strings (0 - 1)
/// https://www.geeksforgeeks.org/jaro-and-jaro-winkler-similarity/
private func jaroWinklerSimilarity(_ s1: [Character], _ s2: [Character]) -> Double {
var jaro = jaroSimilarity(s1, s2)
@nicklockwood
nicklockwood / main.swift
Created May 18, 2020
A simple one-file language parser and compiler test
View main.swift
import Foundation
enum LexingError: Error, Equatable {
case syntaxError(String)
case unexpectedEOF
}
enum ParsingError: Error {
case expected(String)
case unexpectedToken(Token)
View RNG.swift
private let multiplier: UInt64 = 6364136223846793005
private let increment: UInt64 = 1442695040888963407
public struct RNG: RandomNumberGenerator {
private var seed: UInt64 = 0
public init(seed: UInt64) {
self.seed = seed
}
@nicklockwood
nicklockwood / RNG.swift
Created May 17, 2020
RNG not working as expected
View RNG.swift
struct RNG: RandomNumberGenerator {
let modulus: UInt64 = 233_280
let multiplier: UInt64 = 9301
let increment: UInt64 = 49297
var seed: UInt64 = 0
mutating func next() -> UInt64 {
seed = (seed * multiplier + increment) % modulus
return seed
}
View ZeroDefaultingFail.swift
import Foundation
@propertyWrapper
struct ZeroDefaulting: Codable {
var wrappedValue: Int?
init(wrappedValue: Int?) {
print("set:", wrappedValue ?? "nil")
self.wrappedValue = wrappedValue
}
@nicklockwood
nicklockwood / AnyFoo.swift
Last active Mar 17, 2021
POC for serializing heterogeneous array of structs using Codable in Swift
View AnyFoo.swift
import Foundation
enum FooType: String, Codable {
case bar, baz
}
protocol Foo: Codable {
var type: FooType { get }
}