Last active
March 13, 2021 20:39
-
-
Save gtokman/29df9468fe346315f27429076c3120cb to your computer and use it in GitHub Desktop.
Overload == operator
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
| typealias Predicate<Element> = (Element) -> Bool | |
| func ==<Element>(lhs: KeyPath<Element, String>, rhs: String) -> Predicate<Element> { // overload == | |
| return { element in | |
| guard !rhs.isEmpty else { return true } // make sure not empty | |
| return search( | |
| needle: rhs.lowercased(), // search | |
| haystack: element[keyPath: lhs].lowercased() // value for keypath | |
| ) | |
| } | |
| } | |
| // ... | |
| List( | |
| friends | |
| .filter(\.name == searchText) // 💯 | |
| .prefix(30) | |
| ) | |
| // ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment