Skip to content

Instantly share code, notes, and snippets.

View vincent-pradeilles's full-sized avatar

Vincent Pradeilles vincent-pradeilles

View GitHub Profile
Trigo.cachedCos(.pi * 2) // takes 48.85 µs
// value of cos for 2π is now cached
Trigo.cachedCos(.pi * 2) // takes 0.13 µs
struct Trigo {
@Cached static var cachedCos = { (x: Double) in cos(x) }
}
@propertyWrapper
struct Cached<Input: Hashable, Output> {
private var cachedFunction: (Input) -> Output
init(wrappedValue: @escaping (Input) -> Output) {
self.cachedFunction = Cached.addCachingLogic(to: wrappedValue)
}
var wrappedValue: (Input) -> Output {
@propertyWrapper
struct Cached<Input: Hashable, Output> {
private var cachedFunction: (Input) -> Output
var wrappedValue: (Input) -> Output {
get { return self.cachedFunction }
}
}
@propertyWrapper
struct Cached<Input: Hashable, Output> {
private var cachedFunction: (Input) -> Output
}
struct Cached<Input: Hashable, Output> {
private var cachedFunction: (Input) -> Output
}
var printer: (String) -> Void = { print($0) }