Skip to content

Instantly share code, notes, and snippets.

Nick Lockwood nicklockwood

Block or report user

Report or block nicklockwood

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@nicklockwood
nicklockwood / AnyFoo.swift
Last active Sep 26, 2019
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 }
}
@nicklockwood
nicklockwood / AnyFoo.swift
Last active Sep 18, 2019
POC for serializing heterogeneous array of classes using Codable in Swift
View AnyFoo.swift
import Foundation
enum FooType: String, Codable {
case bar, baz
}
protocol Foo: AnyObject, Codable {
var type: FooType { get }
}
View Withable.swift
/// Withable is a simple protocol to make constructing
/// and modifying objects with multiple properties
/// more pleasant (functional, chainable, point-free)
public protocol Withable {
init()
}
public extension Withable {
/// Construct a new instance, setting an arbitrary subset of properties
init(with config: (inout Self) -> Void) {
View gist:35bc1734f2f8c97177010b1188345921
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) ->
guard let obj = (self.representedObject as? ModelType)?.collection(at:row) else {
return nil
}
switch tableColumn?.identifier.rawValue ?? "" {
case "column1":
return obj.field1
case "column2":
return objc.field2
//...
@nicklockwood
nicklockwood / StringsTest.swift
Last active Jan 16, 2019
string concat benchmarks
View StringsTest.swift
import XCTest
let count = 50000
class StringsTestTests: XCTestCase {
// MARK: Copying
func testInterpolation() {
var foo = ""
@nicklockwood
nicklockwood / main.swift
Created Dec 1, 2017
Thread-safe cache implementation benchmark
View main.swift
var cache = [Int: Int]()
let queue = DispatchQueue(label: "cacheQueue", attributes: .concurrent)
let iterations = 100000
// In the first run, cache is empty so we're writing each time
do {
let start = CFAbsoluteTimeGetCurrent()
for i in 0 ... iterations {
var exists = false
queue.sync {
@nicklockwood
nicklockwood / UnicodeScalarView.swift
Last active Sep 21, 2017
UnicodeScalarView.swift
View UnicodeScalarView.swift
// This is a really simple drop-in replacement for String.UnicodeScalarView
// As of Swift 3.2, String.UnicodeScalarView no longer supports slice operations, and
// String.UnicodeScalarView.Subsequence is ~5x slower
//
// Only a small subset of methods are implemented, specifically the ones useful for
// implementing a parser or lexer that consumes a string by repeatedly calling popFirst()
//
// I've benchmarked popFirst() as ~7x faster than String.UnicodeScalarView.Subsequence in Swift 3.2 and 4.0
// The performance is close to that of String.UnicodeScalarView in Swift 3.1, but may be slightly worse in some use cases
View Pattern for Swift Errors.swift
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)
@nicklockwood
nicklockwood / error-propagation.swift
Last active Aug 17, 2017
Typed error use-case example
View error-propagation.swift
enum ValueError: Error {
case notFound
case typeMismatch
}
struct PropertyError: Error {
let name: String
let error: ValueError
}
View json.md

A very basic JSON parser, written in [new language]. Only supports String, Int and Array so far.

json := "[42, \"hello\", [\"good\", \"bye\"]]"

JSON = String|Int|[JSON] // TODO: true, false, null, object

skipSpace(json: String) String {
    input := json
    while input.first == " " {
You can’t perform that action at this time.