Skip to content

Instantly share code, notes, and snippets.

@gtokman
Last active March 13, 2021 20:39
Show Gist options
  • Select an option

  • Save gtokman/29df9468fe346315f27429076c3120cb to your computer and use it in GitHub Desktop.

Select an option

Save gtokman/29df9468fe346315f27429076c3120cb to your computer and use it in GitHub Desktop.
Overload == operator
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