Last active
June 30, 2019 16:34
-
-
Save ollieatkinson/bdea009d6a19ee688163ac97ae238e72 to your computer and use it in GitHub Desktop.
A definition of logical conditions used to constrain a search for in-memory filtering
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
public func == <Root, Value: Equatable>(keyPath: KeyPath<Root, Value>, value: Value) -> (Root) -> Bool { | |
return { $0[keyPath: keyPath] == value } | |
} | |
public func != <Root, Value: Equatable>(keyPath: KeyPath<Root, Value>, value: Value) -> (Root) -> Bool { | |
return { $0[keyPath: keyPath] != value } | |
} | |
public func < <Root, Value: Comparable>(keyPath: KeyPath<Root, Value>, value: Value) -> (Root) -> Bool { | |
return { $0[keyPath: keyPath] < value } | |
} | |
public func > <Root, Value: Comparable>(keyPath: KeyPath<Root, Value>, value: Value) -> (Root) -> Bool { | |
return { $0[keyPath: keyPath] > value } | |
} | |
public func <= <Root, Value: Comparable>(keyPath: KeyPath<Root, Value>, value: Value) -> (Root) -> Bool { | |
return { $0[keyPath: keyPath] <= value } | |
} | |
public func >= <Root, Value: Comparable>(keyPath: KeyPath<Root, Value>, value: Value) -> (Root) -> Bool { | |
return { $0[keyPath: keyPath] >= value } | |
} | |
public func && <T>(lhs: @escaping @autoclosure () -> Bool, rhs: @escaping (T) -> Bool) -> (T) -> Bool { | |
return { lhs() && rhs($0) } | |
} | |
public func || <T>(lhs: @escaping @autoclosure () -> Bool, rhs: @escaping (T) -> Bool) -> (T) -> Bool { | |
return { lhs() || rhs($0) } | |
} | |
public func && <T>(lhs: @escaping (T) -> Bool, rhs: @escaping @autoclosure () -> Bool) -> (T) -> Bool { | |
return { lhs($0) && rhs() } | |
} | |
public func || <T>(lhs: @escaping (T) -> Bool, rhs: @escaping @autoclosure () -> Bool) -> (T) -> Bool { | |
return { lhs($0) || rhs() } | |
} | |
public func && <T>(lhs: @escaping (T) -> Bool, rhs: @escaping (T) -> Bool) -> (T) -> Bool { | |
return { lhs($0) && rhs($0) } | |
} | |
public func || <T>(lhs: @escaping (T) -> Bool, rhs: @escaping (T) -> Bool) -> (T) -> Bool { | |
return { lhs($0) || rhs($0) } | |
} |
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
//: Playground - noun: a place where people can play | |
struct User { | |
let name: String | |
let team: String | |
let numberOfCommits: Int | |
} | |
let bob = User(name: "Bob", team: "Blue", numberOfCommits: 8) | |
let alice = User(name: "Alice", team: "Red", numberOfCommits: 18) | |
let charlie = User(name: "Charlie", team: "Yellow", numberOfCommits: 6) | |
let scott = User(name: "Scott", team: "Yellow", numberOfCommits: 12) | |
let adam = User(name: "Adam", team: "Blue", numberOfCommits: 13) | |
let alice2 = User(name: "Alice", team: "Red", numberOfCommits: 3) | |
let bobsName = bob[keyPath: \User.name] | |
let users = [ bob, alice, charlie, scott, adam, alice2 ] | |
// Find all users in the blue team | |
users.filter(\User.team == "Blue") | |
// Find all users in the blue team and numberOfCommits is larger than 10 | |
users.filter(\User.team == "Blue" && \User.numberOfCommits > 10) | |
// Find all users in the blue team, or users who are called alice and have a commit count over 5 | |
users.filter(\User.team == "Blue" || \User.name == "Alice" && \User.numberOfCommits > 5) | |
let isWednesday = false | |
// Find all users in the blue team only on Wednesday, or users who are called alice and have a commit count over 5 | |
users.filter(\User.team == "Blue" && isWednesday || \User.name == "Alice" && \User.numberOfCommits > 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment