Swiftpack.co -  YOCKOW/SwiftPredicate as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
YOCKOW/SwiftPredicate
Treat predicates abstractly.
.package(url: "https://github.com/YOCKOW/SwiftPredicate.git", from: "1.2.1")

What is SwiftPredicate?

You can abstractly treat predicates with this library.

Requirements

  • Swift 5 (Also OK is compatibility mode for Swift 4 or 4.2)
  • macOS or Linux

Dependency

Usage

Simple Predicate

import Predicate

struct SimplePredicate<Variable>: PredicateProtocol {
  var _predicate: (Variable) -> Bool
  
  init(_ predicate:@escaping (Variable) -> Bool) {
    self._predicate = predicate
  }
  
  func evaluate(with argument: Variable) -> Bool {
    return self._predicate(argument)
  }
}

let lessThan10 = SimplePredicate<Int>({ $0 < 10 })
let greaterThan0 = SimplePredicate<Int>({ $0 > 0 })

// `PredicateProtocol` provides some operations like below:

print(lessThan10.and(greaterThan0).evaluate(with:5)) // Prints "true" 
print(lessThan10.and(greaterThan0).evaluate(with:-5)) // Prints "false"

print(lessThan10.or(greaterThan0).evaluate(with:15)) // Prints "true" 

print(lessThan10.xor(greaterThan0).evaluate(with:-5)) // Prints "true" 
print(lessThan10.xor(greaterThan0).evaluate(with:5)) // Prints "false"

A set defined by a predicate

There is also a set named "TotallyOrderedSet" that conforms to SetAlgebra and ConsolidatablePredicate (that inherits from PredicateProtocol). You can define elements contained by the set using ranges. (See SwiftRanges if you want to know what AnyRange is.)

import Predicate
import Ranges

let set1 = TotallyOrderedSet<Double>(elementsIn:[
  AnyRange<Double>(..<0.0),
  AnyRange<Double>(1.0...2.0),
  AnyRange<Double>(3.0<..)
])

let set2 = TotallyOrderedSet<Double>(elementsIn:[
  AnyRange<Double>((-2.0)<..(-1.0)),
  AnyRange<Double>(0.5..<1.5),
  AnyRange<Double>(2.0<..<3.5)
])

print(set1.inverted ==
      TotallyOrderedSet<Double>(elementsIn:[
        AnyRange<Double>(0.0..<1.0),
        AnyRange<Double>(2.0<..3.0),
      ])
     )
// Prints "true"

print(set1.intersection(set2) ==
      TotallyOrderedSet<Double>(elementsIn:[
        AnyRange<Double>((-2.0)<..(-1.0)),
        AnyRange<Double>(1.0..<1.5),
        AnyRange<Double>(3.0<..<3.5),
      ])
     )
// Prints "true"

print(set1.union(set2) ==
      TotallyOrderedSet<Double>(elementsIn:[
        AnyRange<Double>(..<0.0),
        AnyRange<Double>(0.5...),
      ])
     )
// Prints "true"

License

MIT License.
See "LICENSE.txt" for more information.

GitHub

link
Stars: 2
Last commit: 5 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API