Skip to content

Instantly share code, notes, and snippets.

@feighter09
Last active July 25, 2017 02:30
Show Gist options
  • Save feighter09/31c426c1dc897d86f8a71f5ad8f02b80 to your computer and use it in GitHub Desktop.
Save feighter09/31c426c1dc897d86f8a71f5ad8f02b80 to your computer and use it in GitHub Desktop.
import Foundation
struct Person {
var firstName: String
var lastName: String
var age: Int
}
extension Person {
enum Query {
case firstName(String)
case lastName(String)
case age(Int)
func compare(person: Person) -> Bool
{
switch self {
case .firstName(let firstName):
return person.firstName == firstName
case .lastName(let lastName):
return person.lastName == lastName
case .age(let age):
return person.age == age
}
}
}
}
class PhoneBook {
let people = [Person(firstName: "Austin", lastName: "Feight")]
func search(by options: [Person.Query]) -> Person?
{
let query = buildQuery(from: options)
return people.filter(query).first
}
func buildQuery(from options: [Person.Query]) -> (Person) -> Bool
{
return { person in
return options.map { $0.compare(person: person) }
.reduce(true, &&)
}
}
}
PhoneBook().search(by: [.firstName("Austin"), .lastName("Feight")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment