Adrian May | 17 May 2013 19:51
Picon

Fish tank monad

Hi folks,

I think I need a monad but I'm not sure which, and maybe something simpler would do.

I want to model a fish tank as a function of time onto how many fish there are in it at that time. If I already have such a function, I want to operate on it with something like "add 2 fish on day 20" or "take 3 away on day 15" to get a new function of the same form, but the latter should not remove more fish than there are in the tank at that time, and it should tell me how many I get. I don't promise to apply these operators in chronological order.

This seems like the kind of thing that would be in the prelude somewhere. But where?

TIA,
Adrian.


<div><div dir="ltr">Hi folks,<div><br></div>
<div>I think I need a monad but I'm not sure which, and maybe something simpler would do.</div>
<div><br></div>
<div>I want to model a fish tank as a function of time onto how many fish there are in it at that time. If I already have such a function, I want to operate on it with something like "add 2 fish on day 20" or "take 3 away on day 15" to get a new function of the same form, but the latter should not remove more fish than there are in the tank at that time, and it should tell me how many I get. I don't promise to apply these operators in chronological order.</div>
<div><br></div>
<div>This seems like the kind of thing that would be in the prelude somewhere. But where?</div>
<div><br></div>
<div>TIA,</div>
<div>Adrian.</div>
<div><br></div>
<div><br></div>
</div></div>
Daniel Díaz Casanueva | 15 May 2013 23:44
Picon
Favicon

Invitation to connect on LinkedIn

 
 
Mihai,
 
 
 
 
 
 
 
 
 
 
 
 
 
Daniel Díaz Casanueva wants to connect with you on LinkedIn.
 
 
Daniel Díaz Casanueva
Computer Software Professional View Profile »
 
 
 
 
 
 
 
 
You are receiving Invitation emails. Unsubscribe.
 
This email was intended for Mihai Maruseac (Student at Universitatea 'Politehnica' din Bucuresti). Learn why we included this. © 2013, LinkedIn Corporation. 2029 Stierlin Ct. Mountain View, CA 94043, USA
 
<div>

  

  
    <table border="0" cellspacing="0" cellpadding="0" width="100%" bgcolor="#F4F4F4"><tr><td align="center">

<table border="0" cellspacing="0" cellpadding="0" bgcolor="#f4f4f4" width="100%" class=""><tr><td width="98%" valign="top" align="center">
  <table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" width="590" class="responsive"><tr><td width="98%" valign="top" align="">
    <table border="0" cellspacing="0" cellpadding="0" width="100%" bgcolor="#f4f4f4" class="responsive"><tr>
<td align="right" width="590" class="responsive">
          <table cellspacing="0" cellpadding="0" border="0"><tr>
<td height="15"></td>
              </tr></table>
<div>
            <a href="http://www.linkedin.com/">
              </a>
          </div>
        </td>
      </tr></table>
<table border="0" cellspacing="0" cellpadding="0" width="100%" class="responsive"><tr>
<td bgcolor="#ffffff" class="responsive" align="left">
            <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff" width="100%"><tr>
<td width="15"><table width="15" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table></td>
                <td>
                  Mihai,
                </td>
                <td width="15"><table width="15" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table></td>
              </tr></table>
<table bgcolor="#f2faff" width="100%">
<tr>
<td height="15">
                    <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                  <td height="15">
                    <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                  <td height="15">
                    <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                  <td height="15">
                    <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                </tr>
<tr><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>
<td width="15">
                        <table width="15" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                      <td>
                        <table border="0" cellspacing="0" cellpadding="0" width="100%" bgcolor="#f2faff">
<tr>
<td height="10">
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                            <td height="10">
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                            <td height="10">
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                            <td height="10">
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                            <td height="10">
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                          </tr>
<tr>
<td width="10">
                              <table width="10" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                              <td valign="top" width="60">
                                  </td>
                              <td width="10">
                                <table width="10" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                            <td valign="top">
                              <div>
                                Daniel D&iacute;az Casanueva wants to connect with you on LinkedIn.
                              </div>
                              <div>
                                <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</div>
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
<div>Daniel D&iacute;az Casanueva</div>
                              <table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>
<td>
                                    <table border="0" cellspacing="0" cellpadding="0" width="50%" class="responsive-50per" align="left"><tr>
<td>Computer Software Professional</td>
                                        <td>
                                          <a href="http://www.linkedin.com/e/-3dpk6t-hgr1eda3-6o/rso/255311896/LIky/name/25134867_I684271679_25/eml-comm_invm-b-pro_txt-inv28/?hs=false&amp;tok=2hT0ivqF8o4lM1">
                                            <span>
                                              View Profile &raquo;
                                            </span>
                                          </a>
                                        </td>
                                      </tr></table>
</td>
                                </tr></table>
</td>
                            <td width="10">
                              <table width="10" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                          </tr>
<tr>
<td height="10">
                              <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                          </tr>
</table>
</td>
                      <td width="15">
                        <table width="15" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                    </tr></table></tr>
<tr>
<td>
                    <table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="10">
                          <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                      </tr>
<tr>
<td>
                          <div>
                            <table border="0" cellspacing="1" cellpadding="3" align="left"><tr>
<td width="5">
                                  <table width="5" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                                <td colspan="3" align="left">
                                    <table border="0" cellpadding="6" cellspacing="1" align=""><tr><td align="center" valign="middle" bgcolor="#FFE86C"><div><a href="http://www.linkedin.com/e/-3dpk6t-hgr1eda3-6o/uyFflI7IwvrmSA8-0MFfTI7lXX8s3O9lyQ/blk/I684271679_25/3wOtCVFbmdxnSVFbm8JrnpKqlZJrmZzbmNJpjRQnOpBtn9QfmhBt71BoSd1p65Lr6lOfPkOnPATdz4TczgUdAALlCoRsAdbdnALc30PcPwNdP0PcP4LrCBxbOYWrSlI/eml-comm_invm-b-in_ac-inv28/?hs=false&amp;tok=2FzMHzzo0o4lM1"><span>Accept</span></a></div></td></tr></table>
</td>
                              </tr></table>
</div>
                        </td>
                      </tr>
<tr>
<td height="10">
                          <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
                      </tr>
</table>
</td>
                </tr>
</table>
<table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>
</td>
        </tr></table>
</td></tr></table>
</td></tr></table>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="#f4f4f4" width="100%" class=""><tr><td width="98%" valign="top" align="center">

<table border="0" cellspacing="0" cellpadding="0" width="580" class="responsive">
<tr><td><table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table></td></tr>
<tr>
<td align="left">
            You are receiving Invitation emails. <a href="https://www.linkedin.com/e/-3dpk6t-hgr1eda3-6o/uyFflI7IwvrmSA8-0MFfTI7lXX8s3O9lyQ/uns/20008/25134867/6o3gdnrgreg8s2d/beginners%40haskell%2Eorg/-3dpk6t-hgr1eda3-6o/eml-comm_invm-f-unsub-inv28/?hs=false&amp;tok=2LnCJ8-Eoo4lM1">Unsubscribe</a>.
        <table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table>

                This email was intended for Mihai Maruseac (Student at Universitatea 'Politehnica' din Bucuresti). <a href="http://www.linkedin.com/e/-3dpk6t-hgr1eda3-6o/plh/http%3A%2F%2Fhelp%2Elinkedin%2Ecom%2Fapp%2Fanswers%2Fdetail%2Fa_id%2F4788/-GXI/?hs=false&amp;tok=0pRQwEVKAo4lM1">Learn why we included this</a>. &copy; 2013, LinkedIn Corporation. 2029 Stierlin Ct. Mountain View, CA 94043, USA
    </td>
  </tr>
<tr><td><table width="1" border="0" cellspacing="0" cellpadding="0" class=""><tr><td><div>&nbsp;</div></td></tr></table></td></tr>
</table>
</td></tr></table>
</td></tr></table>
</div>
irfan hudda | 15 May 2013 20:04
Picon

HLint fails to give suggestions

I install HLint via cabal (output http://hpaste.org/88021)
To check if it was working I used following code 

fun1 :: Int -> String
fun1 1 = "hell"
fun1 2 = 3

and ran HLint on it
$ ~/.cabal/bin/hlint hlint_test.hs
No suggestions

and it ouputs no suggestions
any ideas?

Additionally
$ ~/.cabal/bin/hlint -v
HLint v1.8.45, (C) Neil Mitchell 2006-2012


<div><div dir="ltr">
<div>
<div>
<div>I install HLint via cabal (output <a href="http://hpaste.org/88021">http://hpaste.org/88021</a>)<br>
</div>To check if it was working I used following code&nbsp; <br><br>fun1 :: Int -&gt; String<br>
fun1 1 = "hell"<br>fun1 2 = 3<br><br>
</div>and ran HLint on it<br>$ ~/.cabal/bin/hlint hlint_test.hs <br>No suggestions<br><br>
</div>and it ouputs no suggestions<br>any ideas?<br><br>Additionally<br>$ ~/.cabal/bin/hlint -v<br>
HLint v1.8.45, (C) Neil Mitchell 2006-2012<br><br><div><br></div>
</div></div>
Julian Arni | 15 May 2013 19:09

find by

(Truncated again - trying plain text. Sorry for the spam.)

I have a function that, simplifying a little, looks like this:

  fn :: [a] -> a
  fn = (head .) . takeWhile $ (\_ -> True)

From this, I want a function with the signature fn' :: [(x,a)] ->
(x,a) such that:

    snd $ fn' (zip [x] [a]) = fn [a]

I can see ways of doing this by altering fn, or by repeating some of
fn in the definition of fn', or (because in this case I know that if
fn xs = x, fn is returning the first x in xs and not any others), by
doing something nasty like:

  fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -> a == fn (snd $
unzip xs)) xs )

But it seems to me like there should be prettier solutions to this
(that do not involve changing the definition of fn). After all, this
doesn't seem like a rare pattern.

Anyone know if in fact there's a better way to go about it?

Julian Arni | 15 May 2013 19:05

find by

(For some reason my previous post seems to have been truncated.)

I have a function that, simplifying a little, looks like this:

  fn :: [a] -> a
  fn = (head .) . takeWhile $ (\_ -> True)

From this, I want a function with the signature fn' :: [(x,a)] -> (x,a) such that:

    snd $ fn' (zip [x] [a]) = fn [a]

I can see ways of doing this by altering fn, or by repeating some of fn in the definition of fn', or (because in this case I know that if fn xs = x, fn is returning the first x in xs and not any others), by doing something nasty like:

  fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -> a == fn (snd $ unzip xs)) xs )

But it seems to me like there should be prettier solutions to this (that do not involve changing the definition of fn). After all, this doesn't seem like a rare pattern.

Anyone know if in fact there's a better way to go about it?
<div><div dir="ltr">
<div><span>(For some reason my previous post seems to have been truncated.)</span></div>
<span><div>
<span><br></span>
</div>I have a function that, simplifying a little, looks like this:</span><div><br></div>
<div>
&nbsp; fn :: [a] -&gt; a</div>
<div>&nbsp; fn = (head .) . takeWhile $ (\_ -&gt; True)</div>
<div><br></div>
<div>
From this, I want a function with the signature fn' :: [(x,a)] -&gt; (x,a) such that:</div>
<div><br></div>
<div>&nbsp; &nbsp; snd $ fn' (zip [x] [a]) = fn [a]</div>
<div><br></div>
<div>I can see ways of doing this by altering fn, or by repeating some of fn in the definition of fn', or (because in this case I know that if fn xs = x, fn is returning the first x in xs and not any others), by doing something nasty like:</div>
<div><br></div>
<div>&nbsp; fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -&gt; a == fn (snd $ unzip xs)) xs )<br>
</div>
<div><br></div>
<div>But it seems to me like there should be prettier solutions to this (that&nbsp;do not&nbsp;involve changing the definition of fn). After all, this doesn't seem like a rare pattern.</div>
<div><br></div>
<div>Anyone know if in fact there's a better way to go about it?</div>
</div></div>
Julian Arni | 15 May 2013 18:11

find by

I have a function that, simplifying a little, looks like this:

  fn :: [a] -> a
  fn = (head .) . takeWhile $ (\_ -> True)

From this, I want a function with the signature fn' :: [(x,a)] -> (x,a) such that:

    snd $ fn' (zip [x] [a]) = fn [a]

I can see ways of doing this by altering fn, or by repeating some of fn in the definition of fn', or (because in this case I know that if fn xs = x, fn is returning the first x in xs and not any others), by doing something nasty like:

  fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -> a == fn (snd $ unzip xs)) xs )

But it seems to me like there should be prettier solutions to this (that do not involve changing the definition of fn). After all, this doesn't seem like a rare pattern.

Anyone know if in fact there's a better way to go about it?

<div><div dir="ltr">I have a function that, simplifying a little, looks like this:<div><br></div>
<div>&nbsp; fn :: [a] -&gt; a</div>
<div>&nbsp; fn = (head .) . takeWhile $ (\_ -&gt; True)</div>
<div><br></div>
<div>From this, I want a function with the signature fn' :: [(x,a)] -&gt; (x,a) such that:</div>

<div><br></div>
<div>&nbsp; &nbsp; snd $ fn' (zip [x] [a]) = fn [a]</div>
<div><br></div>
<div>I can see ways of doing this by altering fn, or by repeating some of fn in the definition of fn', or (because in this case I know that if fn xs = x, fn is returning the first x in xs and not any others), by doing something nasty like:</div>
<div><br></div>
<div>&nbsp; fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -&gt; a == fn (snd $ unzip xs)) xs )<br>
</div>
<div><br></div>
<div>But it seems to me like there should be prettier solutions to this (that do not&nbsp;involve changing the definition of fn). After all, this doesn't seem like a rare pattern.</div>
<div><br></div>
<div>Anyone know if in fact there's a better way to go about it?</div>
<div><br></div>
</div></div>
Costello, Roger L. | 15 May 2013 11:44
Picon
Favicon

Please help with this basic lexical analyzer program, implemented using the "Alex" tool

Hi Folks,

I am learning how to use the Haskell lexical analyzer tool called "Alex" [1].

I am trying to implement a lexical analyzer for this string (an email "From:" header):

	From: "John Doe" <john <at> doe.org>

I want to break it up into this list of tokens:

[
  From,
  DisplayName "John Doe",
  Email,
  LocalName "john",
  Domain "doe.org"
]

Below is my implementation. It works fine if the string doesn't contain a display name. That is, this works fine:

let s = "From: <john <at> doe.org>"
alexScanTokens s

However, when I include a display name, I get this error message:

[From*** Exception: lexical error

That is, this results in an error:

let s = "From: \"John Doe\" <john <at> doe.org>"
alexScanTokens s

I am guessing that this part of my "Alex" program is causing the error:

\"[a-zA-Z ]+\" 		{ \s -> DisplayName (init (tail s)) }

In Alex the left side is a regular expression:

	\"[a-zA-Z ]+\"

and the right side is the action to be taken when a string is found that matches the regular expression:

	{ \s -> DisplayName (init (tail s)) }

Any thoughts on what the problem might be?

Here is my lexical analyzer program:
-------------------------------------------------
{
module Main (main) where
}

%wrapper "basic"

$digit = 0-9			-- digits
$alpha = [a-zA-Z]		-- alphabetic characters

tokens :-

  $white+				;
  From:					{ \s -> From }
  \"[a-zA-Z ]+\" 				{ \s -> DisplayName (init (tail s)) }
  \<        					{ \s -> Email }
  [$alpha]+ <at> 				{ \s -> LocalPart (init s) }
  [$alpha\.]+>	   			{ \s -> Domain (init s) }

{
-- Each action has type :: String -> Token

-- The token type:
data Token =
	From 			|
	DisplayName String  	|
	Email			|
	LocalPart String	|
	Domain String		
	deriving (Eq,Show)

main = do
  s <- getContents
  print (alexScanTokens s)
}
-------------------------------------------------            
[1] The "Alex" lexical analyzer tool may be found at this URL: http://www.haskell.org/alex/doc/html/introduction.html

Adrian May | 15 May 2013 05:06
Picon

Diagrams brain twister

Hi all,

I'm trying to draw a picture with diagrams (this isn't the gantt chart I was talking about before.)

I have a load of objects strewn around a diagram according to their own sweet logic, and for *some* of them, I want to draw a horizontal line going from the right hand edge of the object to some globally fixed x coordinate, call it the "margin". So those lines are all different lengths because the objects are all over the place, but their right-hand ends should all be aligned vertically.

This seems quite hard, because that sweet logic is already quite complicated and local to a set of objects in the immediate neighbourhood of the object in question. Somehow I have to tease out a selection of them and process each of them into this line whose properties depend on where the object is from the global perspective.

But how?

Adrian.



<div><div dir="ltr">Hi all,<div><br></div>
<div>I'm trying to draw a picture with diagrams (this isn't the gantt chart I was talking about before.)</div>
<div><br></div>
<div>
<div>I have a load of objects strewn around a diagram according to their own sweet logic, and for *some* of them, I want to draw a horizontal line going from the right hand edge of the object to some globally fixed x coordinate, call it the "margin". So those lines are all different lengths because the objects are all over the place, but their right-hand ends should all be aligned vertically.</div>
<div><br></div>
<div>This seems quite hard, because that sweet logic is already quite complicated and local to a set of objects in the immediate neighbourhood of the object in question. Somehow I have to tease out a selection of them and process each of them into this line whose properties depend on where the object is from the global perspective.</div>
<div><br></div>
<div>But how?</div>
<div><br></div>
<div>Adrian.</div>
<div><br></div>
<div><br></div>
<div><br></div>
</div>
</div></div>
Giacomo Tesio | 14 May 2013 11:22
Picon
Favicon
Gravatar

How to improve lazyness of a foldl (and memory footprint)

Hi, I'm trying to improve a small haskell program of mine.

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




<div><div dir="ltr">Hi, I'm trying to improve a small haskell program of mine.<div>A more extended description with full source code is here:&nbsp;<a href="http://codereview.stackexchange.com/questions/26107/how-to-improve-readability-and-memory-footprint-of-this-haskell-script">http://codereview.stackexchange.com/questions/26107/how-to-improve-readability-and-memory-footprint-of-this-haskell-script</a>
</div>
<div><br></div>
<div>The script transforms CSV files into other CSV files but looks like it's reading the whole input files before writing output files.</div>
<div><br></div>
<div>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.</div>
<div><br></div>
<div>But what I can't understand is why this design doesn't work:</div>
<div><br></div>
<div>
<span class="">transformFile </span><span class="">::</span><span class=""> FilePath </span><span class="">-&gt;</span><span class=""> </span><span class="">([</span><span class="">String</span><span class="">]</span><span class=""> </span><span class="">-&gt;</span><span class=""> a</span><span class="">)</span><span class=""> </span><span class="">-&gt;</span><span class=""> </span><span class="">(</span><span class="">a </span><span class="">-&gt;</span><span class=""> IO r</span><span class="">)</span><span class=""> </span><span class="">-&gt;</span><span class=""> IO r
transformFile file operation continuation </span><span class="">=</span><span class=""> withFile file ReadMode </span><span class="">(\</span><span class="">h </span><span class="">-&gt;</span><span class=""> hGetContents h </span><span class="">&gt;&gt;=</span><span class=""> </span><span class="">(</span><span class="">continuation</span><span class="">.</span><span class="">operation</span><span class="">.</span><span class="">lines</span><span class="">))</span><span class="">
</span>
</div>
<div>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.<br>
</div>
<div><br></div>
<div>Here the relevant parts:</div>
<div><br></div>
<div>
<span class="">importTrades </span><span class="">::</span><span class=""> FilePath </span><span class="">-&gt;</span><span class=""> FilePath </span><span class="">-&gt;</span><span class=""> IO </span><span class="">()</span><span class="">
importTrades outDir csvFile </span><span class="">=</span><span class=""> transformFile csvFile </span><span class="">(</span><span class="">foldTradingSample</span><span class="">.</span><span class="">getTickWriteTrades</span><span class="">)</span><span class=""> </span><span class="">(</span><span class="">saveTradingSamples outDir</span><span class="">)</span><span class="">
    </span><span class="">where</span><span class=""> getTickWriteTrades </span><span class="">=</span><span class=""> filter </span><span class="">(</span><span class="">isBetween </span><span class="">(</span><span class="">9</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">)</span><span class=""> </span><span class="">(</span><span class="">18</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">)).(</span><span class="">catMaybes</span><span class="">.(</span><span class="">map fromCSVLine</span><span class="">))</span><span class="">
          foldTradingSample </span><span class="">=</span><span class=""> foldl toTradingSample </span><span class="">[]</span><span class="">
</span>
</div>
<div>This is the folding function:<br>
</div>
<div><br></div>
<div>
<span class="">toTradingSample </span><span class="">::</span><span class=""> </span><span class="">[</span><span class="">TradingSample</span><span class="">]</span><span class=""> </span><span class="">-&gt;</span><span class=""> Tw.Trade </span><span class="">-&gt;</span><span class=""> </span><span class="">[</span><span class="">TradingSample</span><span class="">]</span><span class="">
toTradingSample </span><span class="">(</span><span class="">current</span><span class="">:</span><span class="">others</span><span class="">)</span><span class=""> twTrade
    </span><span class="">|</span><span class=""> newEqt </span><span class="">==</span><span class=""> equity current </span><span class="">&amp;&amp;</span><span class=""> newDay </span><span class="">==</span><span class=""> day current </span><span class="">=</span><span class=""> </span><span class="">(</span><span class="">current </span><span class="">{</span><span class=""> trades </span><span class="">=</span><span class=""> newTrades </span><span class="">}):</span><span class="">others
    </span><span class="">|</span><span class=""> otherwise </span><span class="">=</span><span class=""> current </span><span class="">:</span><span class=""> toTradingSample others twTrade
    </span><span class="">where</span><span class=""> newEqt </span><span class="">=</span><span class=""> Tw.tSimbol twTrade
          newDay </span><span class="">=</span><span class=""> Tw.tDate twTrade
          newTrade </span><span class="">=</span><span class=""> fromTickWrite twTrade
          newTrades </span><span class="">=</span><span class=""> trades current </span><span class="">++</span><span class=""> </span><span class="">[</span><span class="">newTrade</span><span class="">]</span><span class="">
toTradingSample </span><span class="">[]</span><span class=""> twTrade </span><span class="">=</span><span class=""> </span><span class="">[</span><span class="">TradingSample </span><span class="">{</span><span class=""> equity </span><span class="">=</span><span class=""> Tw.tSimbol twTrade
                                            </span><span class="">,</span><span class=""> day </span><span class="">=</span><span class=""> Tw.tDate twTrade
                                            </span><span class="">,</span><span class=""> trades </span><span class="">=</span><span class=""> </span><span class="">[</span><span class="">fromTickWrite twTrade</span><span class="">]</span><span class="">
                                            </span><span class="">}]</span>
</div>
<div>And this is the function that safe the fold results to files</div>
<div><br></div>
<div>
<span class="">saveTradingSamples </span><span class="">::</span><span class=""> String </span><span class="">-&gt;</span><span class=""> </span><span class="">[</span><span class="">TradingSample</span><span class="">]</span><span class=""> </span><span class="">-&gt;</span><span class=""> IO </span><span class="">()</span><span class="">
saveTradingSamples folder samples </span><span class="">=</span><span class=""> mapM_ </span><span class="">(</span><span class="">saveTradingSample folder</span><span class="">)</span><span class=""> samples

saveTradingSample </span><span class="">::</span><span class=""> String </span><span class="">-&gt;</span><span class=""> TradingSample </span><span class="">-&gt;</span><span class=""> IO </span><span class="">()</span><span class="">
saveTradingSample folder sample </span><span class="">=</span><span class=""> writeFile fileName contents
    </span><span class="">where</span><span class=""> fileName </span><span class="">=</span><span class=""> folder </span><span class="">++</span><span class=""> </span><span class="">"\\"</span><span class=""> </span><span class="">++</span><span class=""> </span><span class="">(</span><span class="">equity sample</span><span class="">)</span><span class=""> </span><span class="">++</span><span class=""> </span><span class="">"_"</span><span class=""> </span><span class="">++</span><span class=""> </span><span class="">(</span><span class="">formatTime defaultTimeLocale </span><span class="">"%F"</span><span class=""> </span><span class="">$</span><span class=""> day sample</span><span class="">)</span><span class=""> </span><span class="">++</span><span class=""> </span><span class="">".CSV"</span><span class="">
          contents </span><span class="">=</span><span class=""> tradingSampleToCSV sample</span>
</div>
<div><br></div>
<div>What's wrong here?</div>
<div><br></div>
<div>My insight is that the problem is in the signature of transform files, that requires to completely compute the list of TradingSample before calling&nbsp;<span class="">saveTradingSamples.</span>
</div>
<div>Is this the problem? How can I fix this?<span class=""><br></span>
</div>
<div><br></div>
<div><br></div>
<div>Giacomo</div>
<div><br></div>
<div><br></div>
<div><br></div>
<div><br></div>
</div></div>
Adrian May | 14 May 2013 11:21
Picon

Unmaybe

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.


<div><div dir="ltr">Hi there,<div><br></div>
<div>I have a really annoying scrap of code:</div>
<div><br></div>
<div>
<div>unmaybe Nothing = mempty</div>
<div>unmaybe (Just dia) = dia</div>
<div><br></div>
<div>
It happened because I'm using Diagrams but building my diagram requires looking something up in a list using findIndex, which returns Maybe Int.</div>
<div><br></div>
<div>How do I rid myself of this blotch?</div>
</div>
<div><br></div>
<div>TIA,</div>
<div>Adrian.</div>
<div><br></div>
<div><br></div>
</div></div>
Libor Wagner | 11 May 2013 11:10
Picon

Read file from sys

Hi, 

I have small code snippet to read content of a file, which should grow into simple tool to read temperature
from sensor on Raspberry Pi:

module Main where

import System.Environment 

main :: IO ()
main = do 
[f] <- getArgs 
s <- readFile f 
putStr s 

When this is called with the test file I have copied from the real location it works fine:

$ cat /sys/bus/w1/devices/28-000004e46240/w1_slave >test.txt
$ ./ds18b20 test.txt 
53 01 4b 46 7f ff 0d 10 e9 : crc=e9 YES
53 01 4b 46 7f ff 0d 10 e9 t=21187

How ever when called with the real location nothing happens:

$ ./ds18b20 /sys/bus/w1/devices/28-000004e46240/w1_slave

Can someone help? I have thought that the file is not properly ended so I have tried to take just first line but
that doest work either.

Thanks
Libor


Gmane