Created
November 30, 2011 07:05
-
-
Save ikedaisuke/1408300 to your computer and use it in GitHub Desktop.
Yet another program in Haskell to pretty-print and parse files
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
module Main where | |
-- see also Shugo's blog | |
-- http://shugo.net/jit/20111129.html#p01 | |
import Control.Applicative | |
import Data.List | |
import System.Environment | |
import System.IO | |
import qualified Text.ParserCombinators.Parsec as PC | |
import qualified Text.PrettyPrint as PP | |
data Assessment = Assessment { | |
applicant :: String, | |
title :: String, | |
productivity :: Int, | |
originality :: Int, | |
feasibility :: Int, | |
judgesPoint :: Int | |
} deriving (Show) | |
-- use label of record | |
totalPoint :: Assessment -> Int | |
totalPoint x | |
= productivity x + originality x + feasibility x + | |
judgesPoint x | |
-- use combinators of pretty printer | |
pretty :: Assessment -> String | |
pretty x = PP.render $ PP.hcat $ intersperse PP.comma | |
[ PP.text (applicant x) | |
, PP.text (title x) | |
, PP.int (productivity x) | |
, PP.int (originality x) | |
, PP.int (feasibility x) | |
, PP.int (judgesPoint x) | |
, PP.int (totalPoint x) ] | |
-- remove do-notation (revised; use *> instead of >>) | |
point :: PC.Parser Int | |
point = PC.char '-' *> return 0 PC.<|> | |
read <$> PC.many PC.digit | |
pointWithComma :: PC.Parser Int | |
pointWithComma = point <* PC.char ',' | |
text :: PC.Parser String | |
text = PC.many $ PC.noneOf "," | |
textWithComma :: PC.Parser String | |
textWithComma = text <* PC.char ',' | |
record :: PC.Parser Assessment | |
record = Assessment <$> textWithComma <*> textWithComma <*> | |
pointWithComma <*> pointWithComma <*> | |
pointWithComma <*> point | |
assessmentSummary :: [String] -> [Assessment] | |
assessmentSummary = undefined -- same as shugo's | |
-- remove do-notation, but, use Applicative style | |
readLines :: FilePath -> IO [String] | |
readLines path | |
= lines <$> (openFile path ReadMode >>= hGetContents) | |
header :: String | |
header = "Applicant Name,Project Title,Productivity," ++ | |
"Originality,Feasibility,Judges Point,Total" | |
-- remove do-notation, however, it goes too extreme... | |
main :: IO () | |
main = putStrLn header >> | |
assessmentSummary <$> concat <$> | |
(getArgs >>= mapM readLines) | |
>>= mapM_ (print . pretty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment