Skip to content

Instantly share code, notes, and snippets.

@Kdan
Created February 9, 2019 13:18
Show Gist options
  • Select an option

  • Save Kdan/c59ef7c562647c2c0381170ec76b14da to your computer and use it in GitHub Desktop.

Select an option

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.
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