packages feed

money (empty) → 0.1.0

raw patch · 8 files changed

+370/−0 lines, 8 filesdep +basedep +doctestsetup-changed

Dependencies added: base, doctest

Files

+ CHANGELOG.md view
@@ -0,0 +1,1 @@+# 0.1.0
+ LICENSE.md view
@@ -0,0 +1,22 @@+MIT License++Copyright (c) 2016 Juan Pedro Villa Isaza++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,6 @@+# money++[![][1]][0]++[0]: https://circleci.com/gh/jpvillaisaza/money+[1]: https://circleci.com/gh/jpvillaisaza/money.svg?style=svg
+ Setup.hs view
+ money.cabal view
@@ -0,0 +1,63 @@+name: money+version: 0.1.0++build-type: Simple+cabal-version: >= 1.10++license: MIT+license-file: LICENSE.md++copyright: 2016 Juan Pedro Villa Isaza+author: Juan Pedro Villa Isaza <jpvillaisaza@gmail.com>+maintainer: Juan Pedro Villa Isaza <jpvillaisaza@gmail.com>++stability: Experimental++homepage: https://github.com/jpvillaisaza/money+bug-reports: https://github.com/jpvillaisaza/money/issues++synopsis: Money+description: Money.++category: Finance++tested-with: GHC == 8.0.1++extra-source-files:+  CHANGELOG.md+  README.md+++library+  hs-source-dirs:+      src+  exposed-modules:+      Data.Money+      Data.Money.Currency+  build-depends:+      base >= 4.9 && < 4.10+  default-language:+      Haskell2010+  ghc-options:+      -Wall+++test-suite examples+  type:+      exitcode-stdio-1.0+  hs-source-dirs:+      test/examples+  main-is:+      Main.hs+  build-depends:+      base+    , doctest >= 0.11 && < 0.12+  default-language:+      Haskell2010+  ghc-options:+      -Wall -threaded -rtsopts -with-rtsopts=-N+++source-repository head+  type: git+  location: https://github.com/jpvillaisaza/money
+ src/Data/Money.hs view
@@ -0,0 +1,220 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE RecordWildCards #-}++----------------------------------------------------------------------+-- |+-- Module: Data.Money+--+--+--+----------------------------------------------------------------------++module Data.Money+  ( -- * Currencies+    module Data.Money.Currency+    -- * Money+  , Money (..)+    -- * Exchange rates+  , ExchangeRate (..)+    -- * Interchanges+  , interchange+    -- * Conversions+  , convert+  , convert'+  )+  where++-- money+import Data.Money.Currency+++----------------------------------------------------------------------+-- * Money+----------------------------------------------------------------------++-- |+--+-- Money in a currency.+--+-- Examples:+--+-- >>> 1000 :: Money COP+-- COP 1000.0+--+-- >>> 1000 :: Money EUR+-- EUR 1000.0+--+-- >>> 1000 :: Money USD+-- USD 1000.0+--+-- >>> 999.99 :: Money USD+-- USD 999.99+--+-- >>> 1000 + 500 :: Money COP+-- COP 1500.0+--+-- >>> (1000 :: Money COP) + (500 :: Money EUR)+-- ...+--+-- >>> 1000 == (500 * 2 :: Money USD)+-- True+--+-- >>> 1000 < (500 :: Money EUR)+-- False++newtype Money currency =+  Money+    { getAmount :: Rational+    }+  deriving (Eq, Fractional, Num, Ord)+++-- |+--+-- Show money in COP.++instance Show (Money COP) where+  show Money {..} =+    "COP " ++ show (fromRational getAmount :: Double)+++-- |+--+-- Show money in EUR.++instance Show (Money EUR) where+  show Money {..} =+    "EUR " ++ show (fromRational getAmount :: Double)+++-- |+--+-- Show money in USD.++instance Show (Money USD) where+  show Money {..} =+    "USD " ++ show (fromRational getAmount :: Double)+++----------------------------------------------------------------------+-- * Exchange rates+----------------------------------------------------------------------++-- |+--+-- An exchange rate, that is, the value of one currency for the+-- purpose of conversion to another.+--+-- Examples:+--+-- >>> 3167.20 :: ExchangeRate USD COP+-- COP 3167.2+--+-- >>> 1.06 :: ExchangeRate EUR USD+-- USD 1.06+--+-- >>> 0.94 :: ExchangeRate USD EUR+-- EUR 0.94++newtype ExchangeRate currency1 currency2 =+  ExchangeRate+    { getExchangeRate :: Money currency2+    }+  deriving (Eq, Fractional, Num)+++-- |+--+--++instance Show (Money cur2) => Show (ExchangeRate cur1 cur2) where+  show =+    show . getExchangeRate+++----------------------------------------------------------------------+-- * Interchanges+----------------------------------------------------------------------++-- |+--+-- Interchange (or flip) an exchange rate.+--+-- Examples:+--+-- >>> interchange (1.06 :: ExchangeRate EUR USD)+-- EUR 0.94...+--+-- >>> interchange (0.94 :: ExchangeRate USD EUR)+-- USD 1.06...+--+-- >>> interchange (interchange (1.06 :: ExchangeRate EUR USD))+-- USD 1.06+--+-- >>> interchange (interchange (0.94 :: ExchangeRate USD EUR))+-- EUR 0.94++interchange+  :: ExchangeRate cur1 cur2+  -> ExchangeRate cur2 cur1+interchange (ExchangeRate exchangeRate) =+  ExchangeRate (Money (1 / getAmount exchangeRate))+++----------------------------------------------------------------------+-- * Conversions+----------------------------------------------------------------------++-- |+--+-- Convert money using an exchange rate.+--+-- Examples:+--+-- >>> usdToCop = 3182.01 :: ExchangeRate USD COP+--+-- >>> convert usdToCop 1000+-- COP 3182010.0+--+-- >>> convert (interchange usdToCop) 1000+-- USD 0.31...+--+-- >>> convert (interchange usdToCop) (convert usdToCop 1000)+-- USD 1000.0++convert+  :: ExchangeRate cur1 cur2+  -> Money cur1+  -> Money cur2+convert (ExchangeRate exchangeRate) (Money amount) =+  Money (amount * getAmount exchangeRate)+++-- |+--+-- Convert money using an interchanged (or flipped) exchange rate.+--+-- Examples:+--+-- >>> usdToCop = 3182.01 :: ExchangeRate USD COP+--+-- >>> convert' usdToCop 1000+-- USD 0.31...+--+-- >>> convert' (interchange usdToCop) 1000+-- COP 3182010.0+--+-- >>> convert' usdToCop (convert' (interchange usdToCop) 1000)+-- USD 1000.0+--+-- >>> convert' usdToCop (convert usdToCop 1000)+-- USD 1000.0++convert'+  :: ExchangeRate cur2 cur1+  -> Money cur1+  -> Money cur2+convert' exchangeRate =+  convert (interchange exchangeRate)
+ src/Data/Money/Currency.hs view
@@ -0,0 +1,39 @@+----------------------------------------------------------------------+-- |+-- Module: Data.Money.Currency+--+--+--+----------------------------------------------------------------------++module Data.Money.Currency+  ( COP+  , EUR+  , USD+  )+  where+++----------------------------------------------------------------------+-- * Currencies+----------------------------------------------------------------------++-- |+--+-- The Colombian peso.++data COP+++-- |+--+-- The euro.++data EUR+++-- |+--+-- The United States dollar.++data USD
+ test/examples/Main.hs view
@@ -0,0 +1,19 @@+module Main+  ( main+  )+  where++-- doctest+import Test.DocTest (doctest)+++-- |+--+--++main :: IO ()+main =+  doctest+    [ "-isrc"+    , "src/Data/Money.hs"+    ]