-
-
Save maximvl/3788b14b8e7021cb063a to your computer and use it in GitHub Desktop.
temperature converter
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
| module Tconverter | |
| export convert | |
| import Base.convert | |
| abstract Temperature | |
| type Celsius <: Temperature | |
| value :: Real | |
| end | |
| type Kelvin <: Temperature | |
| value :: Real | |
| end | |
| type Fahrenheit <: Temperature | |
| value :: Real | |
| end | |
| function convert(::Type{Kelvin}, a::Celsius) | |
| Kelvin(a.value + 273.15) | |
| end | |
| function convert(::Type{Fahrenheit}, a::Celsius) | |
| Fahrenheit((a.value * 1.8) + 32) | |
| end | |
| function convert(::Type{Celsius}, a::Kelvin) | |
| Celsius(a.value - 273.15) | |
| end | |
| function convert(::Type{Fahrenheit}, a::Kelvin) | |
| t = convert(Celsius, a) | |
| convert(Fahrenheit, t) | |
| end | |
| function convert(::Type{Celsius}, a::Fahrenheit) | |
| Celsius((a.value - 32) * 5/9) | |
| end | |
| function convert(::Type{Kelvin}, a::Fahrenheit) | |
| t = convert(Celsius, a) | |
| convert(Kelvin, t) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment