Skip to content

NoahKamara/CompoundPredicate

Repository files navigation

Build & Test

For Developers targeting macOS 14.4 / iOS 17.4 or later:

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

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()

Documentation

The documentation is available here and as Docc archive you can view using Xcode

Feedback

Please feel free to create an Issue, or even better contribute actively by creating a pull request

Implementation Progress

  • Arithmetic (+, -, *, /, %)

    • +, -, * PredicateExpressions.Arithmetic
    • / PredicateExpressions.FloatDivision
    • / PredicateExpressions.IntDivision
    • % PredicateExpressions.IntRemainder
  • Unary minus - PredicateExpressions.UnaryMinus

  • Range (..., ..<)

    • ... PredicateExpressions.ClosedRange
    • ..< PredicateExpressions.Range
    • x..<z).contains(y) PredicateExpressions.RangeExpressionContains
  • Comparison (<, <=, >, >=, ==, !=)

    • <, <=, >, >= PredicateExpressions.Comparison
    • == PredicateExpressions.Equal
    • != PredicateExpressions.NotEqual
  • Conditionals & Ternary (?:) PredicateExpressions.Conditional

  • Boolean logic (&&, ||, !)

    • && PredicateExpressions.Conjunction
    • || PredicateExpressions.Disjunction
    • ! PredicateExpressions.Negation
  • Swift optionals (?, ??, !, flatMap(_:), if-let expressions)

    • ?, flatMap(_:) PredicateExpressions.OptionalFlatMap
    • ??, if-let PredicateExpressions.NilCoalesce
  • Types (as, as?, as!, is)

    • as? PredicateExpressions.ConditionalCast
    • as, as! PredicateExpressions.ForceCast
  • 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
  • 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
  • String comparisons

    • contains(_:) PredicateExpressions.CollectionContainsCollection
    • localizedStandardContains(_:) PredicateExpressions.StringLocalizedStandardContains
    • caseInsensitiveCompare(_:) PredicateExpressions.StringCaseInsensitiveCompare
    • localizedCompare(_:) PredicateExpressions.StringLocalizedCompare