| Copyright | (c) Tuomas Laakkonen 2016 |
|---|---|
| License | BSD3 |
| Maintainer | pigworts2@gmail.com |
| Stability | Experimental |
| Portability | Non-portable (GHC extensions) |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.Currency.Convert
Description
This module allows values of currency to be converted from one currency to another using exchange rates from various sources.
The basic operation for this module is as follows:
First, get a conversion function (with or getDefaultConverter):getConverter
>>>Converter convert <- getDefaultConverter
Then, construct some value of currency using the convenience functions, and convert it using the convert function you just got:
>>>convert (usd 100) :: EUR90.0 eur
The result type of convert defines what currency the value will be converted into.
- newtype Converter = Converter (forall a b. (KnownSymbol a, KnownSymbol b) => Currency a -> Currency b)
- getConverter :: RateProvider -> IO Converter
- getDefaultConverter :: IO Converter
- unsafeCoerceCurrency :: Currency a -> Currency b
- data Currency s
- data RateProvider = RateProvider String (IO RateDict)
- newtype RateDict = RateDict [(String, Double)]
- defaultProvider :: RateProvider
- backupProvider :: RateProvider
- dictProvider :: String -> [(String, Double)] -> RateProvider
- localProvider :: FilePath -> RateProvider
- fixerIOProvider :: RateProvider
- (<|-|>) :: RateProvider -> RateProvider -> RateProvider
- aud :: Double -> AUD
- bgn :: Double -> BGN
- brl :: Double -> BRL
- cad :: Double -> CAD
- chf :: Double -> CHF
- cny :: Double -> CNY
- czk :: Double -> CZK
- dkk :: Double -> DKK
- gbp :: Double -> GBP
- hkd :: Double -> HKD
- hrk :: Double -> HRK
- huf :: Double -> HUF
- idr :: Double -> IDR
- ils :: Double -> ILS
- inr :: Double -> INR
- jpy :: Double -> JPY
- krw :: Double -> KRW
- mxn :: Double -> MXN
- myr :: Double -> MYR
- nok :: Double -> NOK
- nzd :: Double -> NZD
- php :: Double -> PHP
- pln :: Double -> PLN
- ron :: Double -> RON
- rub :: Double -> RUB
- sek :: Double -> SEK
- sgd :: Double -> SGD
- thb :: Double -> THB
- try :: Double -> TRY
- usd :: Double -> USD
- zar :: Double -> ZAR
- eur :: Double -> EUR
- type AUD = Currency "aud"
- type BGN = Currency "bgn"
- type BRL = Currency "brl"
- type CAD = Currency "cad"
- type CHF = Currency "chf"
- type CNY = Currency "cny"
- type CZK = Currency "czk"
- type DKK = Currency "dkk"
- type GBP = Currency "gbp"
- type HKD = Currency "hkd"
- type HRK = Currency "hrk"
- type HUF = Currency "huf"
- type IDR = Currency "idr"
- type ILS = Currency "ils"
- type INR = Currency "inr"
- type JPY = Currency "jpy"
- type KRW = Currency "krw"
- type MXN = Currency "mxn"
- type MYR = Currency "myr"
- type NOK = Currency "nok"
- type NZD = Currency "nzd"
- type PHP = Currency "php"
- type PLN = Currency "pln"
- type RON = Currency "ron"
- type RUB = Currency "rub"
- type SEK = Currency "sek"
- type SGD = Currency "sgd"
- type THB = Currency "thb"
- type TRY = Currency "try"
- type USD = Currency "usd"
- type ZAR = Currency "zar"
- type EUR = Currency "eur"
Documentation
is a newtype wrapper around the type of conversion functions to avoid ConverterImpredicativeTypes in getConverter
Constructors
| Converter (forall a b. (KnownSymbol a, KnownSymbol b) => Currency a -> Currency b) |
getConverter :: RateProvider -> IO Converter Source
takes a rate provider and returns a getConverter in the ConverterIO monad.
It is used like this:
>>>Converter convert <- getConverter provider
convert can then be used to convert between currencies:
>>>convert (usd 100) :: GBP75.0 gbp
getDefaultConverter :: IO Converter Source
is like getDefaultConverter except no provider needs to be specified (getConverter is used).defaultProvider
unsafeCoerceCurrency :: Currency a -> Currency b Source
can be used to convert between currencies directly without using exchange rates.
Only use this if you know the resulting currency is supported by your unsafeCoerceCurrencyRateProvider
data RateProvider Source
is used by RateProvider to provide a conversion rate dictionary to the resulting conversion function.
getConverter has two fields, the name (RateProvider:: String), and the action (:: IO RateDict).
The name is used in error reporting, and the action is used to provide conversion rates when needed.
There are several prebuilt s - RateProvider, fixerIOProvider, backupProvider, localProvider and dictProvider.defaultProvider
Constructors
| RateProvider String (IO RateDict) |
is the type provided by a RateDict.
It is a mapping from currency codes to currency-to-euro conversion rates.RateProvider
defaultProvider :: RateProvider Source
This is used by RateProvider it first tries fixer.io and then resorts to the backup dictionary.getDefaultConverter
backupProvider :: RateProvider Source
This has a hardcoded dictionary.RateProvider
dictProvider :: String -> [(String, Double)] -> RateProvider Source
This allows a dictionary to be provided.RateProvider
localProvider :: FilePath -> RateProvider Source
This takes a file path and returns a dictionary of rates from the file.
The file must be in the form RateProvider
<name1> <rate1>
<name2> <rate2>
...
fixerIOProvider :: RateProvider Source
This uses the fixer.io API to return exchange rates updated daily by the European Central Bank.RateProvider
(<|-|>) :: RateProvider -> RateProvider -> RateProvider infixr 9 Source
'(<|-|>)' combines two s to create a third.
This provider tries to return the first providers result, but if an exception is raised, it returns the second providers result.RateProvider