-
-
Save oconnelltoby/af160f5efe7b00c5d33887be280d2855 to your computer and use it in GitHub Desktop.
House struct example
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
struct ApartmentTower { | |
struct Concierge { | |
var name: String | |
} | |
struct Renter { | |
var name: String | |
} | |
var concierge: Concierge | |
var renters: [Renter] | |
} |
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
struct House { | |
struct Inhabitant { | |
var name: String | |
} | |
var inhabitants: [Inhabitant] | |
} |
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
extension House { | |
init(manor: Manor) { | |
let staff = manor.staff.map { Inhabitant(name: $0.name) } | |
let owners: [Inhabitant] = [.init(name: manor.lord.name), .init(name: manor.lady.name)] | |
self.init(inhabitants: owners + staff) | |
} | |
init(apartmentTower: ApartmentTower) { | |
let concierge = Inhabitant(name: apartmentTower.concierge.name) | |
let renters = apartmentTower.renters.map { Inhabitant(name: $0.name) } | |
self.init(inhabitants: [concierge] + renters) | |
} | |
} |
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
let manor = Manor( | |
lord: .init(name: "Lord Pennyworth"), | |
lady: .init(name: "Lady Pennyworth"), | |
staff: [.init(name: "Jenkins"), .init(name: "Nanny Jones")] | |
) | |
let apartmentTower = ApartmentTower( | |
concierge: .init(name: "Alex Smith"), | |
renters: [.init(name: "Sam Thomas"), .init(name: "Mary White")] | |
) | |
let street = Street(houses: [.init(manor: manor), .init(apartmentTower: apartmentTower)]) |
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
struct Manor { | |
struct Lord { | |
var name: String | |
} | |
struct Lady { | |
var name: String | |
} | |
struct Staff { | |
var name: String | |
} | |
var lord: Lord | |
var lady: Lady | |
var staff: [Staff] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment