Skip to content

Instantly share code, notes, and snippets.

@lf94
Created March 3, 2015 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf94/41df2c389c164566e1a2 to your computer and use it in GitHub Desktop.
Save lf94/41df2c389c164566e1a2 to your computer and use it in GitHub Desktop.
Original excellente.hs code to calculate Paypal earnings
{-# 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