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
| enum PersonCodingKeys: String, CodingKey { | |
| case name | |
| case pets | |
| } | |
| // CHALLENGE 1: Nested Heterogeneous Array Decoded. | |
| required init(from decoder: Decoder) throws { | |
| let container = try decoder.container(keyedBy: PersonCodingKeys.self) | |
| name = try container.decode(String.self, forKey: .name) | |
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
| extension KeyedDecodingContainer { | |
| /// Decode a heterogeneous list of objects for a given family. | |
| /// - Parameters: | |
| /// - family: The ClassFamily enum for the type family. | |
| /// - key: The CodingKey to look up the list in the current container. | |
| /// - Returns: The resulting list of heterogeneousType elements. | |
| func decode<T : Decodable, U : ClassFamily>(family: U.Type, forKey key: K) throws -> [T] { | |
| var container = try self.nestedUnkeyedContainer(forKey: key) | |
| var list = [T]() |
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
| // CHALLENGE 1: Nested Heterogeneous Array Decoded. | |
| required init(from decoder: Decoder) throws { | |
| let container = try decoder.container(keyedBy: PersonCodingKeys.self) | |
| name = try container.decode(String.self, forKey: .name) | |
| pets = try container.decode(family: PetFamily.self, forKey: .pets) | |
| } |
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
| func totalDistance(of locations: [CLLocation]) -> CLLocationDistance { | |
| var distance: CLLocationDistance = 0.0 | |
| var previousLocation: CLLocation? | |
| locations.forEach { location in | |
| if let previousLocation = previousLocation { | |
| distance += location.distance(from: previousLocation) | |
| } | |
| previousLocation = location | |
| } |
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
| func totalDistance(initialDistance: CLLocationDistance, initialLocation: CLLocation?, locations: inout [CLLocation]) -> CLLocationDistance { | |
| if locations.isEmpty { | |
| return initialDistance | |
| } else if let initialLocation = initialLocation { | |
| let firstLocation = locations.removeFirst() | |
| let distance = firstLocation.distance(from: initialLocation) | |
| return totalDistance(initialDistance: initialDistance + distance, initialLocation: firstLocation, locations: &locations) | |
| } | |
| return totalDistance(initialDistance: initialDistance, initialLocation: locations.removeFirst(), locations: &locations) | |
| } |
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
| let closure: ((CLLocationDistance, CLLocation?), CLLocation) -> (CLLocationDistance, CLLocation?) = { tuple, location in | |
| guard let previousLocation = tuple.1 else { | |
| return (tuple.0, location) | |
| } | |
| return (tuple.0 + location.distance(from: previousLocation), location) | |
| } |
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
| let closure: ((CLLocationDistance, CLLocation?), CLLocation) -> (CLLocationDistance, CLLocation) = { tuple, location in | |
| return (tuple.0 + location.distance(from: tuple.1 ?? location), location) | |
| } |
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
| func totalDistance(of locations: [CLLocation]) -> CLLocationDistance { | |
| return locations.reduce((0.0, nil), { ($0.0 + $1.distance(from: $0.1 ?? $1), $1) }).0 | |
| } |
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
| extension Array where Element: CLLocation { | |
| /// Returns the total distance of the list of CLLocation objects. | |
| var totalDistance: CLLocationDistance { | |
| return reduce((0.0, nil), { ($0.0 + $1.distance(from: $0.1 ?? $1), $1) }).0 | |
| } | |
| } |
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
| import CoreLocation | |
| /// The locations used for the examples. | |
| let locations = [CLLocation(latitude: 10.0, longitude: 12.0), | |
| CLLocation(latitude: 10.1, longitude: 12.0), | |
| CLLocation(latitude: 10.2, longitude: 12.0), | |
| CLLocation(latitude: 10.1, longitude: 12.0)] | |
| /* A BOILERPLATE SOLUTION */ |