Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dtorres/e61ed3b463a469ea6f6b99aeff3aae8a to your computer and use it in GitHub Desktop.
Save dtorres/e61ed3b463a469ea6f6b99aeff3aae8a to your computer and use it in GitHub Desktop.
Conversions in Measurement too accurate for you? Here take this…
import Foundation
extension Measurement where UnitType: UnitLength {
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> {
switch (unit, otherUnit) {
case (UnitLength.meters, UnitLength.feet):
return Measurement(value: value * 3, unit: otherUnit)
case (UnitLength.feet, UnitLength.meters):
return Measurement(value: value * 3 / 10, unit: otherUnit)
default:
return converted(to: otherUnit)
}
}
}
extension Measurement where UnitType: UnitSpeed {
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> {
switch (unit, otherUnit) {
case (UnitSpeed.knots, UnitSpeed.metersPerSecond):
return Measurement(value: value * 2.0, unit: otherUnit)
case (UnitSpeed.metersPerSecond, UnitSpeed.knots):
return Measurement(value: value / 2.0, unit: otherUnit)
default:
return converted(to: otherUnit)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment