This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
@propertyWrapper | |
struct ZeroDefaulting: Codable { | |
var wrappedValue: Int? | |
init(wrappedValue: Int?) { | |
print("set:", wrappedValue ?? "nil") | |
self.wrappedValue = wrappedValue | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum FooType: String, Codable { | |
case bar, baz | |
} | |
protocol Foo: Codable { | |
var type: FooType { get } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum FooType: String, Codable { | |
case bar, baz | |
} | |
protocol Foo: AnyObject, Codable { | |
var type: FooType { get } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
//... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
let count = 50000 | |
class StringsTestTests: XCTestCase { | |
// MARK: Copying | |
func testInterpolation() { | |
var foo = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |