Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Avoid hard-coded KVC collection operators
import Foundation
enum CollectionOperator: String {
case avg
case count
case max
case min
case sum
case unionOfObjects
case distinctUnionOfObjects
case distinctUnionOfArrays
case unionOfArrays
case distinctUnionOfSets
}
precedencegroup Additive {
associativity: left
}
precedencegroup Multiplicative {
associativity: left
higherThan: Additive
}
infix operator => : Multiplicative
func => (lhs: CollectionOperator, rhs: String) -> String {
return "@" + lhs.rawValue + "." + rhs
}
let a = ["2.1", "-10", "2.1", "5", "2.5", "hello"]
let b = a as NSArray
// @avg.doubleValue
b.value(forKeyPath: .avg => #keyPath(String.doubleValue))
// @distinctUnionOfObjects.length
b.value(forKeyPath: .distinctUnionOfObjects => #keyPath(String.length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.