-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Simplification, using top level functions only * Update README.md
- Loading branch information
1 parent
9247e60
commit 0c29980
Showing
14 changed files
with
200 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/.build | ||
/Packages | ||
/*.xcodeproj | ||
DerivedData/ | ||
DerivedData/ | ||
.swiftpm |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,81 @@ | ||
# Predicate.swift | ||
|
||
[![Build Status](https://travis-ci.com/ollieatkinson/Predicate.swift.svg?branch=master)](https://travis-ci.com/ollieatkinson/Predicate.swift) | ||
[![Swift 5.1](https://img.shields.io/badge/swift-5.1-ED523F.svg?style=flat)](https://swift.org/download/) | ||
[![Swift 5.2](https://img.shields.io/badge/swift-5.1-ED523F.svg?style=flat)](https://swift.org/download/) | ||
[![SPM](https://img.shields.io/badge/swift-SPM-ED523F.svg?style=flat)](https://swift.org/package-manager/) | ||
![SPM](https://img.shields.io/badge/os-linux-lightgrey.svg?style=flat) | ||
![macOS](https://img.shields.io/badge/os-macOS-lightgrey.svg?style=flat) | ||
[![Licence](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat)](LICENCE) | ||
|
||
Predicate composition for filtering operators (eg. `filter`, `prefix(while:)` or `drop(while:)`). | ||
Predicate Composition | ||
|
||
## API | ||
|
||
|
||
### Equals | ||
```swift | ||
extension Sequence { | ||
public func filter(_ predicate: Predicate<Element>) -> [Element] | ||
public func prefix(while predicate: Predicate<Element>) -> [Element] | ||
public func drop(while predicate: Predicate<Element>) -> DropWhileSequence<Self> | ||
public func first(where predicate: Predicate<Element>) -> Element? | ||
public func contains(where predicate: Predicate<Element>) -> Bool | ||
public func allSatisfy(_ predicate: Predicate<Element>) -> Bool | ||
} | ||
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 | ||
``` | ||
|
||
## Examples | ||
#### Example | ||
```swift | ||
users.filter(\.name == "Milos") | ||
users.filter(\.name != "Ste") | ||
``` | ||
|
||
A simple predicate | ||
### Less Than & More Than | ||
```swift | ||
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 | ||
```swift | ||
users.filter(\.age > 30) | ||
users.filter(\.age > 25) | ||
users.filter(\.age <= 55) | ||
``` | ||
|
||
or join predicates together to create more complex filters | ||
### Similar to | ||
|
||
```swift | ||
users.filter(\.isClearToFly == false && \.age > 60) | ||
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 | ||
``` | ||
|
||
or check all objects satisfy a predicate requirement. | ||
#### Example | ||
```swift | ||
users.filter(\.age << 30...35) // Age is within the range 30-35 | ||
users.filter(\.age << [ 30, 32, 34 ]) // Age is one of 30, 32 or 34 | ||
users.filter(\.weight == 85 ± 4 //Weight is equal to 85, plus or minus 4, useful for comparing floating points where accuracy is not important | ||
``` | ||
|
||
### Boolean Logic | ||
|
||
```swift | ||
users.allSatisfy(\.age > 20) | ||
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 | ||
``` | ||
|
||
See more examples in the unit tests. | ||
#### Example | ||
|
||
```swift | ||
users.filter(\.isClearToFly && \.name == "Noah") | ||
users.filter(\.age > 30 && \.name != "Noah") | ||
``` | ||
|
||
# Installation | ||
|
||
## SwiftPM: | ||
|
||
```swift | ||
package.append(.package(url: "https://github.com/ollieatkinson/Predicate.swift", from: "0.1.0")) | ||
package.append(.package(url: "https://github.com/ollieatkinson/Predicate.swift", from: "1.0.0")) | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.