Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Lockwood nicklockwood

View GitHub Profile
@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 Jun 11, 2020
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 {
You can’t perform that action at this time.