Skip to content

Instantly share code, notes, and snippets.

View jayrhynas's full-sized avatar

Jayson Rhynas jayrhynas

View GitHub Profile
@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
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)
}
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
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)
}
@jayrhynas
jayrhynas / ValidatingDecodable.swift
Last active November 17, 2021 16:30
Enforce a constant value for decoded fields. Heavliy inspired by: https://gist.github.com/IanKeen/4f7717d7afd266137042b86983d04bc1
protocol DecodableType {
func decode(container: KeyedDecodingContainer<AnyCodingKey>, key: String) throws
}
protocol ValidatingDecodable: Decodable {
init()
}
extension ValidatingDecodable {
init(from decoder: Decoder) throws {
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):
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)
@jayrhynas
jayrhynas / CustomKeyCodable.swift
Last active February 25, 2021 16:23 — forked from IanKeen/CustomKeyCodable.swift
PropertyWrapper: CustomKeyCodable allows defining the keys for decoding _per property_
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()
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] = []