Created
March 3, 2015 13:06
-
-
Save lf94/41df2c389c164566e1a2 to your computer and use it in GitHub Desktop.
Original excellente.hs code to calculate Paypal earnings
This file contains 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
{-# LANGUAGE OverloadedStrings #-} | |
import Data.Csv | |
import Data.Fixed | |
import Data.Vector as Vec (Vector, fromList, sum, length, toList) | |
import Data.ByteString.Lazy as B (readFile) | |
import Control.Monad | |
import Control.Applicative | |
import System.Environment | |
data Receipt = Item String Centi | |
instance Show Receipt where | |
show (Item a b) = Prelude.show a ++ "\n" | |
toCenti :: String -> Centi | |
toCenti a = case result of | |
[(amt,s)] -> amt | |
_ -> 0 | |
where result = reads (filter (',' /=) a) :: [(Centi, String)] | |
toReceipt :: String -> String -> Receipt | |
toReceipt n a = Item n amount | |
where | |
amount = toCenti a | |
instance FromRecord Receipt where | |
parseRecord rec | |
| Vec.length rec >= 10 = liftA2 toReceipt (rec .! 3) (rec .! 8) | |
| otherwise = mzero | |
main :: IO () | |
main = do | |
args <- getArgs | |
bsFile <- B.readFile (args !! 0) | |
receipts <- return $ | |
case (decode HasHeader bsFile :: Either String (Vector Receipt)) of | |
Left errorMsg -> [Item "" (0.00 :: Centi)] | |
Right receipts -> toList receipts | |
print receipts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment