This PR For Swift Foundation adds compound functionality to Predicates like so: If you are targeting only those versions and above you should use this instead:
let notTooShort = #Predicate<Book> { $0.pages > 50 }
let notTooLong = #Predicate<Book> { $0.pages <= 350 }
let titleFilter = #Predicate<Book> { $0.title.contains("Swift") }
let filter = #Predicate<Book> {
(notTooShort.evaluate($0) && notTooLong.evaluate($0)) || titleFilter.evaluate($0)
}
CompoundPredicate aims to improve the Predicate system to enable combining multiple predicates after constructing them:
let notTooShort = #Predicate<Book> {
$0.pages > 50
}
let notTooLong = #Predicate<Book> {
$0.pages <= 350
}
let lengthFilter = [notTooShort, notTooShort].conjunction()
// Match Books that are just the right length
let titleFilter = #Predicate<Book> {
$0.title.contains("Swift")
}
// Match Books that contain "Swift" in the title or
// are just the right length
let filter = [lengthFilter, titleFilter].disjunction()
The documentation is available here and as Docc archive you can view using Xcode
Please feel free to create an Issue, or even better contribute actively by creating a pull request
[x] Arithmetic (+, -, *, /, %)
+
, -
, *
PredicateExpressions.Arithmetic
/
PredicateExpressions.FloatDivision
/
PredicateExpressions.IntDivision
%
PredicateExpressions.IntRemainder
[x] Unary minus -
PredicateExpressions.UnaryMinus
[x] Range (..., ..<)
...
PredicateExpressions.ClosedRange
..<
PredicateExpressions.Range
x..<z).contains(y)
PredicateExpressions.RangeExpressionContains
[x] Comparison (<, <=, >, >=, ==, !=)
<
, <=
, >
, >=
PredicateExpressions.Comparison
==
PredicateExpressions.Equal
!=
PredicateExpressions.NotEqual
[x] Conditionals & Ternary (?:) PredicateExpressions.Conditional
[x] Boolean logic (&&, ||, !)
&&
PredicateExpressions.Conjunction
||
PredicateExpressions.Disjunction
!
PredicateExpressions.Negation
[x] Swift optionals (?, ??, !, flatMap(_:), if-let expressions)
?
, flatMap(_:)
PredicateExpressions.OptionalFlatMap
??
, if-let
PredicateExpressions.NilCoalesce
[x] Types (as, as?, as!, is)
as?
PredicateExpressions.ConditionalCast
as
, as!
PredicateExpressions.ForceCast
[x] Sequence operations (allSatisfy(), filter(), contains(), contains(where:), starts(with:), max(), min())
allSatisfy()
PredicateExpressions.SequenceAllSatisfy
filter()
PredicateExpressions.Filter
[completion:: 2024-03-10]contains()
PredicateExpressions.SequenceContains
[completion:: 2024-03-10]contains(where:)
PredicateExpressions.SequenceContainsWhere
starts(with:)
PredicateExpressions.SequenceStartsWith
min()
PredicateExpressions.SequenceMinimum
[completion:: 2024-03-10]max()
PredicateExpressions.SequenceMaximum
[x] Subscript and member access ([], .)
[0,1][0]
PredicateExpressions.CollectionIndexSubscript
["a": "b"]["a"]
PredicateExpressions.DictionaryKeySubscript
["a": "b"]["a", defaultValue: "b"]
PredicateExpressions.DictionaryKeyDefaultValueSubscript
obj.someKey
PredicateExpressions.KeyPath
[x] String comparisons
contains(_:)
PredicateExpressions.CollectionContainsCollection
localizedStandardContains(_:)
PredicateExpressions.StringLocalizedStandardContains
caseInsensitiveCompare(_:)
PredicateExpressions.StringCaseInsensitiveCompare
localizedCompare(_:)
PredicateExpressions.StringLocalizedCompare