<div dir="ltr"><br><br><div class="gmail_quote">On Mon, Oct 6, 2008 at 7:06 PM, Mike Coleman <span dir="ltr">&lt;<a href="mailto:tutufan@gmail.com">tutufan@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
I could use a little help. &nbsp;I was looking through the Real World<br>
Haskell book and came across a trivial program for summing numbers in<br>
a file. &nbsp;They mentioned that that implementation was very slow, as<br>
it&#39;s based on String&#39;s, so I thought I&#39;d try my hand at converting it<br>
to use lazy ByteString&#39;s. &nbsp;I&#39;ve made some progress, but now I&#39;m a<br>
little stuck because that module doesn&#39;t seem to have a &#39;read&#39; method.<br>
<br>
There&#39;s a readInt method, which I guess I could use, but it returns a<br>
Maybe, and I don&#39;t see how I can easily strip that off.<br>
<br>
So:<br>
<br>
1. &nbsp;Is there an easy way to strip off the Maybe that would allow an<br>
equivalently concise definition for sumFile? &nbsp;I can probably figure<br>
out how to do it with pattern matching and a separate function--I&#39;m<br>
just wondering if there&#39;s a more concise way.</blockquote><div><br>I&#39;m not a ByteString expert, but there should be an easy way to solve this issue of Maybe.<br><br>If you go to hoogle (<a href="http://www.haskell.org/hoogle/">http://www.haskell.org/hoogle/</a>) and type this:<br>
[Maybe a] -&gt; [a]<br>it says:<br><a class="dull" href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html">Data.Maybe</a><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:catMaybes">.catMaybes</a><a class="dull" href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:catMaybes"> :: <span class="c1">[Maybe a]</span> -&gt; <span class="c0">[a]</span></a><br>
<br>As the top search result.<br><br>This means that you can convert any list of maybes into a list of what you want.&nbsp; It just tosses out the Nothings.<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
2. &nbsp;Why doesn&#39;t ByteString implement &#39;read&#39;? &nbsp;Is it just that this<br>
function (like &#39;input&#39; in Python) isn&#39;t really very useful for real<br>
programs?</blockquote><div><br>I think probably for things more complex than parsing ints it&#39;s best to make your own parser?&nbsp; I seem to recall that someone was working on a library of parsing functions based on bytestring?&nbsp; Maybe someone else can comment?<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
3. &nbsp;Why doesn&#39;t ByteString implement &#39;readDouble&#39;, etc.? &nbsp;That is, why<br>
are Int and Integer treated specially? &nbsp;Do I not need readDouble?</blockquote><div><br>I think readInt was mostly implemented because integer reading was needed a lot for benchmarks and programming challenge sites and people noticed it was way slower than needed so someone put in the effort it optimize it.&nbsp; Once it was optimized, that must have satisfied the need for fast number reading.<br>
<br>I would agree that at least for Prelude types it would be nice to have efficient bytestring based parsers.&nbsp; Do we have Read/Show classes specifically for working in bytestrings?&nbsp; Maybe that&#39;s what the world needs in the bytestring api.&nbsp; Then again, I&#39;m not really qualified to comment :)&nbsp; For all I know it already exists.<br>
<br>Jason<br></div></div><br></div>