This file contains 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
Trigo.cachedCos(.pi * 2) // takes 48.85 µs | |
// value of cos for 2π is now cached | |
Trigo.cachedCos(.pi * 2) // takes 0.13 µs |
This file contains 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 Trigo { | |
@Cached static var cachedCos = { (x: Double) in cos(x) } | |
} |
This file contains 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
@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 { |
This file contains 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
@propertyWrapper | |
struct Cached<Input: Hashable, Output> { | |
private var cachedFunction: (Input) -> Output | |
var wrappedValue: (Input) -> Output { | |
get { return self.cachedFunction } | |
} | |
} |
This file contains 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
@propertyWrapper | |
struct Cached<Input: Hashable, Output> { | |
private var cachedFunction: (Input) -> Output | |
} |
This file contains 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 Cached<Input: Hashable, Output> { | |
private var cachedFunction: (Input) -> Output | |
} |
This file contains 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 printer: (String) -> Void = { print($0) } |