Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Unmaybe (Adrian May)
   2.  How to improve lazyness of a foldl (and memory   footprint)
      (Giacomo Tesio)


----------------------------------------------------------------------

Message: 1
Date: Tue, 14 May 2013 17:21:44 +0800
From: Adrian May <[email protected]>
Subject: [Haskell-beginners] Unmaybe
To: "[email protected]" <[email protected]>
Message-ID:
        <cad-ubzgs7eeeez9vuukmx7iodaksglgmknzpojkpzu_ayfo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi there,

I have a really annoying scrap of code:

unmaybe Nothing = mempty
unmaybe (Just dia) = dia

It happened because I'm using Diagrams but building my diagram requires
looking something up in a list using findIndex, which returns Maybe Int.

How do I rid myself of this blotch?

TIA,
Adrian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130514/c059a675/attachment-0001.htm>

------------------------------

Message: 2
Date: Tue, 14 May 2013 11:22:27 +0200
From: Giacomo Tesio <[email protected]>
Subject: [Haskell-beginners] How to improve lazyness of a foldl (and
        memory  footprint)
To: "[email protected]" <[email protected]>
Message-ID:
        <CAHL7psF99L7YM1k=c70f1ovsch11b8wb9rbn8z4+ng7wux_...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi, I'm trying to improve a small haskell program of mine.
A more extended description with full source code is here:
http://codereview.stackexchange.com/questions/26107/how-to-improve-readability-and-memory-footprint-of-this-haskell-script

The script transforms CSV files into other CSV files but looks like it's
reading the whole input files before writing output files.

I guess that the script can be improved in many ways, in readability and
efficiency, thus any suggestion is wellcome as an occasion to learn.

But what I can't understand is why this design doesn't work:

transformFile :: FilePath -> ([String] -> a) -> (a -> IO r) -> IO r
transformFile file operation continuation = withFile file ReadMode (\h
-> hGetContents h >>= (continuation.operation.lines))

This function recieves a path, a function to left fold lines to a new list
of objects and a function to persist the fold output to files.

Here the relevant parts:

importTrades :: FilePath -> FilePath -> IO ()
importTrades outDir csvFile = transformFile csvFile
(foldTradingSample.getTickWriteTrades) (saveTradingSamples outDir)
    where getTickWriteTrades = filter (isBetween (9, 0) (18,
0)).(catMaybes.(map fromCSVLine))
          foldTradingSample = foldl toTradingSample []

This is the folding function:

toTradingSample :: [TradingSample] -> Tw.Trade -> [TradingSample]
toTradingSample (current:others) twTrade
    | newEqt == equity current && newDay == day current = (current {
trades = newTrades }):others
    | otherwise = current : toTradingSample others twTrade
    where newEqt = Tw.tSimbol twTrade
          newDay = Tw.tDate twTrade
          newTrade = fromTickWrite twTrade
          newTrades = trades current ++ [newTrade]
toTradingSample [] twTrade = [TradingSample { equity = Tw.tSimbol twTrade
                                            , day = Tw.tDate twTrade
                                            , trades = [fromTickWrite twTrade]
                                            }]

And this is the function that safe the fold results to files

saveTradingSamples :: String -> [TradingSample] -> IO ()
saveTradingSamples folder samples = mapM_ (saveTradingSample folder) samples

saveTradingSample :: String -> TradingSample -> IO ()
saveTradingSample folder sample = writeFile fileName contents
    where fileName = folder ++ "\\" ++ (equity sample) ++ "_" ++
(formatTime defaultTimeLocale "%F" $ day sample) ++ ".CSV"
          contents = tradingSampleToCSV sample


What's wrong here?

My insight is that the problem is in the signature of transform files, that
requires to completely compute the list of TradingSample before calling
saveTradingSamples.
Is this the problem? How can I fix this?


Giacomo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130514/b7c744f8/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 59, Issue 12
*****************************************

Reply via email to