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
@dynamicMemberLookup | |
struct Partial<Wrapped> { | |
private struct AnyProperty { | |
let keyPath: PartialKeyPath<Wrapped> | |
let value: Any | |
let writer: (inout Wrapped, Any) -> Void | |
init<T>(keyPath: WritableKeyPath<Wrapped, T>, value: T) { | |
self.keyPath = keyPath | |
self.value = value |
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 sortedCount(of input: [String]) -> [(String, Int)] { | |
input.reduce(into: [String: (index: Int, count: Int)]()) { state, str in | |
var info = state[str] ?? (index: state.count, count: 0) | |
info.count += 1 | |
state[str] = info | |
}.sorted { lhs, rhs in | |
lhs.value.index < rhs.value.index | |
}.map { | |
($0.key, $0.value.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
protocol CodingKeyRawValue: Hashable, Codable {} | |
extension String: CodingKeyRawValue {} | |
extension Int: CodingKeyRawValue {} | |
@propertyWrapper | |
struct CodableDictionary<Key: Hashable, Value> where Key: RawRepresentable, Key.RawValue: CodingKeyRawValue { | |
var wrappedValue: [Key: Value] | |
init(wrappedValue: [Key: Value]) { | |
self.wrappedValue = wrappedValue |
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 | |
extension URLSession { | |
private func convert<Value>(_ work: (@escaping (Value?, URLResponse?, Error?) -> Void) -> Void) async throws -> (Value, URLResponse) { | |
try await withCheckedThrowingContinuation { continuation in | |
work { value, response, error in | |
continuation.resume(with: Result { | |
guard let value = value, let response = response else { | |
throw error ?? URLError(.badServerResponse) | |
} |
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
protocol DecodableType { | |
func decode(container: KeyedDecodingContainer<AnyCodingKey>, key: String) throws | |
} | |
protocol ValidatingDecodable: Decodable { | |
init() | |
} | |
extension ValidatingDecodable { | |
init(from decoder: Decoder) throws { |
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 re | |
def findNeedles(haystack, needles): | |
if len(needles) > 5: | |
print "Too many needles" | |
else: | |
countArray = [0] * len(needles) | |
for index, needle in enumerate(needles): |
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
private let configKey = CodingUserInfoKey(rawValue: UUID().uuidString)! | |
private struct ConfigWrapper<Value> { | |
let value: Value | |
} | |
extension ConfigWrapper: Decodable where Value: DecodableWithConfiguration { | |
init(from decoder: Decoder) throws { | |
let config = decoder.userInfo[configKey]! as! Value.DecodingConfiguration | |
value = try Value(from: decoder, configuration: config) |
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
protocol CustomKeyCodable: Codable { | |
static var keyEncodingStrategy: ([CodingKey]) -> CodingKey { get } | |
static var keyDecodingStrategy: ([CodingKey]) -> CodingKey { get } | |
init() | |
} | |
extension CustomKeyCodable { | |
init(from decoder: Decoder) throws { | |
self.init() |

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 | |
class ThreadQueue { | |
private var mutex = pthread_mutex_t() | |
private var cond = pthread_cond_t() | |
// these are shared variables and should only be modified within a `lock` block | |
private var threadId: pthread_t? = nil | |
private var isStopped = true | |
private var queue: [() -> Void] = [] |