From a stock-screen to a Haskell data type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Analytics.Trading.Data.Row where

import Data.Time

import Control.Scan.CSV                          -- http://lpaste.net/109651

import Data.Monetary.USD                         -- http://lpaste.net/109653

-- represents a Row of data from a (stock) security screen, and gives an
-- easy way to read it in from a file as a Read instance.

data Row = Row { date :: Day, open :: USD, high :: USD, low :: USD,
                 close :: USD, volume :: Integer, adjClose :: USD }
     deriving Show

instance Read Row where
     readsPrec _ line = let (date:open:high:low:close:vol:adj:_) = csv line
                        in  [(Row (read date) (mknMoney open) (mknMoney high)
                                  (mknMoney low) (mknMoney close) (read vol)
                                  (mknMoney adj), "")]

readRows :: FilePath -> IO [Row]
readRows file = readFile file >>= return . ans . lines
    where ans (header:rows) = foldr (\ row accum -> read row : accum) [] rows
23:17: Warning: Use liftM
Found:
readFile file >>= return . ans . lines
Why not:
Control.Monad.liftM (ans . lines) (readFile file)