Skip to content

Instantly share code, notes, and snippets.

@maximvl
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save maximvl/3788b14b8e7021cb063a to your computer and use it in GitHub Desktop.

Select an option

Save maximvl/3788b14b8e7021cb063a to your computer and use it in GitHub Desktop.
temperature converter
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