[Haskell-cafe] csv one-liner

Henning Thielemann lemming at henning-thielemann.de
Tue Sep 30 14:48:37 EDT 2008


On Tue, 30 Sep 2008, wman wrote:

> I got asked how to do one particular thing in excel, which led to discssion
> with "our local MSOffice expert".
> During the discussion I stated that's it too much of a PITA and that I'd
> rather write a script.
> Long story short, I promised him a one-liner to "show the power and beauty
> of Haskell".
>
> I got the csv package from hackage, modified the parseCSVFromFile so it's
> returns IO CSV rather than IO (Either ParseError CSV), and finished with
> following code
>
> (writeFile "output.csv") =<< (liftM printCSV $ liftM (map updateLine) $
> parseCSVFromFile "input.csv")
>
> Is there room for improvement ?
> Could it still be made into one-liner without modifying the csv module (and
> without resorting to
> case parseCSVFromFile "input.csv" of { Left _ -> []; Right x -> x}
> kind of tricks) ?

The line will become a little longer, but you can wrap IO (Either ...) in 
an ErrorT, use its exception handling capability and unwrap the complete 
action. Or you replace 'case' on Either by 'either (const []) id' or 
'either (error "could not open file") id'.


More information about the Haskell-Cafe mailing list