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.)
What is
SwiftPredicate
?You can abstractly treat predicates with this library.
Requirements
Dependency
Usage
Simple Predicate
A set defined by a predicate
There is also a set named “TotallyOrderedSet“ that conforms to
SetAlgebra
andConsolidatablePredicate
(that inherits fromPredicateProtocol
). You can define elements contained by the set using ranges. (See SwiftRanges if you want to know whatAnyRange
is.)License
MIT License.
See “LICENSE.txt” for more information.