Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Last active July 16, 2021 08:17
Show Gist options
  • Save ytyubox/75b79ac2ea136bfedbf76f27b04182c9 to your computer and use it in GitHub Desktop.
Save ytyubox/75b79ac2ea136bfedbf76f27b04182c9 to your computer and use it in GitHub Desktop.
// MARK: - EqualKeyPath
struct EqualKeyPath<T, A> where A: Equatable {
init(on: T.Type, for k1: KeyPath<T, A>) {
self.k1 = k1
}
let k1: KeyPath<T, A>
func equalFor(_ a: T, _ b: T) -> Bool {
a[keyPath: k1] == b[keyPath: k1]
}
}
// MARK: - EqualKeyPath2
struct EqualKeyPath2<T, A, B> where A: Equatable, B: Equatable {
let k1: KeyPath<T, A>
let k2: KeyPath<T, B>
func equalFor(_ a: T, _ b: T) -> Bool {
let bools = [
a[keyPath: k1] == b[keyPath: k1],
a[keyPath: k2] == b[keyPath: k2],
]
return bools.allSatisfy { $0 }
}
}
// MARK: - EqualKeyPath3
struct EqualKeyPath3<T, A, B, C> where
A: Equatable,
B: Equatable,
C: Equatable
{
init(on: T.Type,
_ k1: KeyPath<T, A>,
_ k2: KeyPath<T, B>,
_ k3: KeyPath<T, B>)
{
self.k1 = k1
self.k2 = k2
self.k3 = k3
}
let k1: KeyPath<T, A>
let k2: KeyPath<T, B>
let k3: KeyPath<T, B>
func equalFor(_ a: T, _ b: T) -> Bool {
let bools = [
a[keyPath: k1] == b[keyPath: k1],
a[keyPath: k2] == b[keyPath: k2],
a[keyPath: k3] == b[keyPath: k3],
]
return bools.allSatisfy { $0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment