Created
October 22, 2018 18:35
-
-
Save st-small/0cacd26c3cae89d282d843fd9dc34a3a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| protocol Payable { | |
| func calculateWages() -> Int | |
| } | |
| extension Payable { | |
| func calculateWages() -> Int { | |
| return 10000 | |
| } | |
| } | |
| protocol ProvidesTreatment { | |
| func treat(name: String) | |
| } | |
| extension ProvidesTreatment { | |
| func treat(name: String) { | |
| print("I have treated \(name)") | |
| } | |
| } | |
| protocol ProvidesDiagnosis { | |
| func diagnose() -> String | |
| } | |
| extension ProvidesDiagnosis { | |
| func diagnose() -> String { | |
| return "He's dead, Jim" | |
| } | |
| } | |
| protocol ConductSurgery { | |
| func performSurgery() | |
| } | |
| extension ConductSurgery { | |
| func performSurgery() { | |
| print("Success!") | |
| } | |
| } | |
| protocol HasRestTime { | |
| func takeBreak() | |
| } | |
| extension HasRestTime { | |
| func takeBreak() { | |
| print("Time to watch TV") | |
| } | |
| } | |
| protocol NeedsTraining { | |
| func study() | |
| } | |
| extension NeedsTraining { | |
| func study() { | |
| print("I'm reading a book") | |
| } | |
| } | |
| protocol Employee: Payable, HasRestTime, NeedsTraining { } | |
| struct Receptionist { } | |
| struct Nurse { } | |
| struct Doctor { } | |
| struct Surgeon { } | |
| extension Receptionist: Employee { } | |
| extension Nurse: Employee, ProvidesTreatment { } | |
| extension Doctor: Employee, ProvidesTreatment, ProvidesDiagnosis { } | |
| extension Surgeon: Employee, ProvidesDiagnosis, ConductSurgery { } | |
| extension Employee where Self: ProvidesTreatment { | |
| func checkInsurance() { | |
| print("Yup, I'm totally insured") | |
| } | |
| } | |
| extension Collection where Iterator.Element: BinaryInteger { | |
| func countOddEven() -> (odd: Int, even: Int) { | |
| var even = 0 | |
| var odd = 0 | |
| for val in self { | |
| if val % 2 == 0 { | |
| even += 1 | |
| } else { | |
| odd += 1 | |
| } | |
| } | |
| return (odd, even) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment