Last active
July 25, 2017 02:30
-
-
Save feighter09/31c426c1dc897d86f8a71f5ad8f02b80 to your computer and use it in GitHub Desktop.
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
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