Skip to content

Instantly share code, notes, and snippets.

@feighter09

feighter09/Queries.swift

Last active Jul 25, 2017
Embed
What would you like to do?
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