Swift Package Index


A small addition to Swift adding predicate matchers for filtering operators using KeyPaths, it just feels natural™


  • The latest stable release is v1.0.0. Released 7 months ago.
  • The last commit to master was 5 months ago.

Swift Version Compatibility

  • v1.0.0 and master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • v1.0.0 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

Predicate

Swift 5.2 SPM SPM macOS Licence

Predicate Composition

API

Equals

public func == <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Equatable
public func != <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Equatable

Example

users.filter(\.name == "Milos")
users.filter(\.name != "Ste")

Less Than & More Than

public func < <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable
public func > <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable
public func <= <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable
public func >= <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable

Example

users.filter(\.age > 25)
users.filter(\.age <= 55)

Similar to

public func << <Root, S>(block: @escaping (Root) -> S.Element, value: S) -> (Root) -> Bool where S : Sequence, S.Element : Equatable
public func ~= <Root>(block: @escaping (Root) -> String, regex: Regex) -> (Root) -> Bool
public func == <Root, Value>(block: @escaping (Root) -> Value, value: (Value, Value)) -> (Root) -> Bool where Value : FloatingPoint
public func ± <Value>(number: Value, accuracy: Value) -> (Value, Value) where Value : FloatingPoint

Example

  • << sequence contains
  • ± comparing floating point numbers to a degree of accuracy
  • ~= Regex
users.filter(\.age << 30...35)
users.filter(\.age << [ 30, 32, 34 ])
users.filter(\.weight == 85 ± 4
users.filter(\.name ~= "o(s|ah)$")

Boolean Logic

public prefix func ! <Root>(block: @escaping (Root) -> Bool) -> (Root) -> Bool
public func && <Root>(lhs: @autoclosure @escaping () -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool
public func || <Root>(lhs: @autoclosure @escaping () -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool
public func && <Root>(lhs: @escaping (Root) -> Bool, rhs: @autoclosure @escaping () -> Bool) -> (Root) -> Bool
public func || <Root>(lhs: @escaping (Root) -> Bool, rhs: @autoclosure @escaping () -> Bool) -> (Root) -> Bool
public func && <Root>(lhs: @escaping (Root) -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool
public func || <Root>(lhs: @escaping (Root) -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool

Example

users.filter(\.isClearToFly && \.name == "Noah")
users.filter(\.age > 30 && \.name != "Noah")

Installation

SwiftPM:

package.append(.package(url: "https://github.com/ollieatkinson/Predicate", from: "1.0.0"))