Skip to content

Commit

Permalink
Simplification (#11)
Browse files Browse the repository at this point in the history
* Simplification, using top level functions only

* Update README.md
  • Loading branch information
ollieatkinson authored Jun 4, 2020
1 parent 9247e60 commit 0c29980
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 465 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/.build
/Packages
/*.xcodeproj
DerivedData/
DerivedData/
.swiftpm
28 changes: 0 additions & 28 deletions .travis.yml

This file was deleted.

67 changes: 47 additions & 20 deletions README.md
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"))
```
85 changes: 0 additions & 85 deletions Sources/Predicate/KeyPathPartialPredicate.swift

This file was deleted.

33 changes: 0 additions & 33 deletions Sources/Predicate/Operators/Combination.swift

This file was deleted.

19 changes: 0 additions & 19 deletions Sources/Predicate/Operators/Comparable.swift

This file was deleted.

11 changes: 0 additions & 11 deletions Sources/Predicate/Operators/Equatable.swift

This file was deleted.

53 changes: 0 additions & 53 deletions Sources/Predicate/Operators/Like.swift

This file was deleted.

Loading

0 comments on commit 0c29980

Please sign in to comment.