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 | |
import Combine | |
extension Timer { | |
static func publish(at date: Date, tolerance: TimeInterval? = nil, on runLoop: RunLoop, in mode: RunLoop.Mode, options: RunLoop.SchedulerOptions? = nil) -> AnyPublisher<Date, Never> { | |
let subject = PassthroughSubject<Date, Never>() | |
let timer = Timer(fire: date, interval: 0, repeats: false) { _ in | |
subject.send(Date()) | |
subject.send(completion: .finished) |
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
@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 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 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 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
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 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 | |
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 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
protocol DecodableType { | |
func decode(container: KeyedDecodingContainer<AnyCodingKey>, key: String) throws | |
} | |
protocol ValidatingDecodable: Decodable { | |
init() | |
} | |
extension ValidatingDecodable { | |
init(from decoder: Decoder) throws { |
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 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 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 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 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
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() |
