Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
nicklockwood / RNG.swift
Created May 17, 2020 22:23
Working RNG
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 02:21
RNG not working as expected
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
}
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 March 17, 2021 17:57
POC for serializing heterogeneous array of structs using Codable in Swift
import Foundation
enum FooType: String, Codable {
case bar, baz
}
protocol Foo: Codable {
var type: FooType { get }
}
@nicklockwood
nicklockwood / AnyFoo.swift
Last active September 18, 2019 00:00
POC for serializing heterogeneous array of classes using Codable in Swift
import Foundation
enum FooType: String, Codable {
case bar, baz
}
protocol Foo: AnyObject, Codable {
var type: FooType { get }
}
@nicklockwood
nicklockwood / Withable.swift
Created January 28, 2019 12:06
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) {
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 May 4, 2021 04:47
string concat benchmarks
import XCTest
let count = 50000
class StringsTestTests: XCTestCase {
// MARK: Copying
func testInterpolation() {
var foo = ""
@nicklockwood
nicklockwood / main.swift
Created December 1, 2017 11:04
Thread-safe cache implementation benchmark
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 May 30, 2021 13:12
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