Skip to content

Instantly share code, notes, and snippets.

View ollieatkinson's full-sized avatar
🌴
On vacation

Oliver Atkinson ollieatkinson

🌴
On vacation
View GitHub Profile
@ollieatkinson
ollieatkinson / JSON.swift
Created March 15, 2021 09:42
Another JSON box implementation over `Any` type
struct JSON {
enum Index {
case ordinal(Int), key(String)
}
private(set) var any: Any?
init(_ any: Any? = nil) {
self.any = any
@ollieatkinson
ollieatkinson / KeyValuePairs.swift
Created March 8, 2021 09:16
Key value pairing with support for equality, hashing and coding
extension Dictionary {
public typealias KeyValuePairs = [KeyValuePair<Key, Value>]
public var keyValuePairs: KeyValuePairs { map(KeyValuePair.init) }
}
extension Collection where Element: KeyValuePair_P {
public var dictionary: [Element.Key: Element.Value] { Dictionary(uniqueKeysWithValues: map(\.tuple)) }
}
public protocol KeyValuePair_P {
@ollieatkinson
ollieatkinson / AnyDictionary.swift
Last active December 26, 2020 20:12
AnyDictionary
protocol AnyDictionary: Collection {
associatedtype Key: Hashable
associatedtype Keys
associatedtype Value
associatedtype Values
associatedtype Index
var keys: Keys { get }
var values: Values { get }
@ollieatkinson
ollieatkinson / AnyResult.swift
Last active December 23, 2020 22:04
AnyResult
protocol AnyResult {
associatedtype Success
associatedtype Failure: Error
static func success(_ success: Success) -> Self
static func failure(_ failure: Failure) -> Self
func map<NewSuccess>(_ transform: (Success) -> NewSuccess) -> Result<NewSuccess, Failure>
func mapError<NewFailure>(_ transform: (Failure) -> NewFailure) -> Result<Success, NewFailure> where NewFailure : Error
@ollieatkinson
ollieatkinson / FileSystemEventStream.swift
Created October 22, 2020 10:27
sane `fsevents` in Swift
import Foundation
import Combine
public struct Event: Identifiable {
public let id: FSEventStreamEventId
public let path: String
public let flags: Flags
}
public class FileSystemEventStream {
@ollieatkinson
ollieatkinson / JSON.swift
Last active December 29, 2020 17:09
Hacking around with JSON represented as an indirect enum
import Foundation
public indirect enum JSON: Hashable {
public enum Leaf: Hashable {
case null
case boolean(Bool)
case number(NSNumber)
case string(String)
case error(Error)
@ollieatkinson
ollieatkinson / Dictionary+KeyPath.swift
Last active January 7, 2024 03:30
KeyPath to get/set from a Swift JSON object ([String: Any] or [Any])
import Foundation
extension Dictionary where Key == String, Value == Any {
public subscript(keyPath: JSON.Path.Index...) -> Value? {
get { self[keyPath: .init(path: keyPath)] }
set { self[keyPath: .init(path: keyPath)] = newValue }
}
public subscript(keyPath keyPath: JSON.Path) -> Value? {
@ollieatkinson
ollieatkinson / JSONObject.swift
Last active April 8, 2024 21:41
JSON Equatable in Swift
public let null = NSNull() as AnyHashable
public enum JSONObject {
case array([Any])
case dictionary([String: Any])
case fragment(Any)
}
extension JSONObject {
@inlinable public static func with(_ data: Data, options: JSONSerialization.ReadingOptions = []) throws -> JSONObject {
@ollieatkinson
ollieatkinson / AnyEquatable.swift
Last active September 30, 2020 21:22
Equatable for Any
extension Equatable {
public static func isEqual(_ l: Any, _ r: Any) -> Bool {
(l as? Self).map { isEqual($0, r) } ?? false
}
public static func isEqual(_ l: Self, _ r: Any) -> Bool {
isEqual(r, l)
}
public static func isEqual(_ l: Any, _ r: Self) -> Bool {
(l as? Self).map{ $0 == r } ?? false
}
@ollieatkinson
ollieatkinson / NotificationCenter.swift
Last active June 14, 2022 11:39
Making NotificationCenter easier to work with and allowing type-safe rules
extension NotificationCenter {
public func observe(
name: NSNotification.Name,
object obj: Any? = nil,
queue: OperationQueue? = .main,
using block: @escaping (Notification?) -> Void
) -> Notification.Token {
return Notification.Token(
on: self,