Created
February 9, 2019 13:18
-
-
Save Kdan/c59ef7c562647c2c0381170ec76b14da to your computer and use it in GitHub Desktop.
An example of how a recursive total distance function can be implemented on a list of CLLocation objects.
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) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment