Skip to content

Instantly share code, notes, and snippets.

@YanSte
Last active August 18, 2022 08:02
Show Gist options
  • Save YanSte/1a418c04f6376811cca1b5f3b6f28cbf to your computer and use it in GitHub Desktop.
Save YanSte/1a418c04f6376811cca1b5f3b6f28cbf to your computer and use it in GitHub Desktop.
Predicate, A definition of logical conditions for constraining a search for a fetch or for in-memory filtering
import Foundation
/// A definition of logical conditions for constraining a search for a fetch or for in-memory filtering.
/// More info [here](https://www.swiftbysundell.com/articles/predicates-in-swift/)
///
/// Sample:
///
/// func myMethod(predicate: Predicate<MyObject>) -> Bool {
/// objects.contains { predicate(obj) }
/// }
///
/// let result = myMethod { $0.id == myIdDefined }
///
public struct Predicate<Target> {
public var matches: (Target) -> Bool
public init(matcher: @escaping (Target) -> Bool) {
self.matches = matcher
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment