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 Lock { | |
private var unfairLock = os_unfair_lock_s() | |
mutating func lock(_ work: () -> Void) { | |
self.lock() | |
work() | |
self.unlock() | |
} | |
mutating func trylock(_ work: () -> Void) -> Bool { |
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
import ObjectiveC | |
extension objc_AssociationPolicy { | |
public static let retain = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN | |
public static let retainNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC | |
public static let copy = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY | |
public static let copyNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC | |
public static let assign = objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN | |
} |
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
import Foundation | |
/// Allows a property to fail decoding, defaulting to nil instead of surfacing the error. | |
@propertyWrapper | |
struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable { | |
var wrappedValue: Wrapped? | |
init() { | |
self.wrappedValue = nil | |
} |
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
func _if<T>(_ test: Bool, action: () -> T) -> T? { | |
if test { | |
return action() | |
} else { | |
return nil | |
} | |
} | |
extension Optional { | |
func _else(action: () -> Wrapped) -> Wrapped { |
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
extension Collection where Element: FloatingPoint { | |
func average() -> Element { | |
return self.reduce(Element.zero, +) / Element(self.count) | |
} | |
} | |
extension Collection where Element: BinaryInteger { | |
func average() -> Double { | |
return Double(self.reduce(0, +)) / Double(self.count) | |
} |
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
import PlaygroundSupport | |
let limiter = RateLimiter(timeframe: 5) | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
for i in 0..<100 { | |
limiter.perform { | |
print("perform", i) | |
} |
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 Parent: View { | |
@State var count: Int = 0 | |
var body: some View { | |
return VStack { | |
ImageLoadingView(url: URL(string: "https://github.com/recurser/exif-orientation-examples/blob/9c4ccfaea6bfd434ac1c4bb0750ac6fc5848a5f4/Landscape_8.jpg?raw=true")!) | |
Text("counter: \(count)") | |
} | |
.onAppear { | |
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { (timer) in | |
self.count += 1 |
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
/// The result of a binary search. | |
struct BinarySearchResult<Index> { | |
/// The index for the searched key. | |
/// | |
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true. | |
var index: Index | |
/// Whether the key was found in the collection. | |
/// | |
/// When performing a binary search, even if the value is not found in the collection, an index is returned which can be used to insert a new item at the correct location. |
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
// adapted from https://github.com/raywenderlich/swift-algorithm-club/tree/master/Binary%20Search | |
/// The result of a binary search. | |
struct BinarySearchResult<Index> { | |
/// The index for the searched key. | |
/// | |
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true. | |
var index: Index | |
/// Whether the key was found in the collection. |
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
import Foundation | |
import SQLite3 | |
public enum SQLiteError: Error, LocalizedError { | |
case sqlite(code: Int32, message: String?) | |
case invalidDatabase | |
case invalidStatement | |
case invalidUTF8String | |