module Currency.Rates where
import Prelude hiding (lookup)
import Data.Map (Map, empty, lookup, insert)
data Rates a b = Rates {
reference :: a,
rates :: Map a b
} deriving (Show, Read, Eq)
rebase :: (Ord a, Fractional b) => a -> Rates a b -> Rates a b
rebase new rs@(Rates old m)
| new == old = rs
| otherwise = Rates new $ maybe empty
(\newRate -> fmap (/newRate) $ insert old 1 m) $
lookup new m
exchangeRate :: (Ord a, Fractional b) =>
Rates a b
-> a
-> a
-> Maybe b
exchangeRate rs source target = lookup target $ rates (rebase source rs)