[Haskell-cafe] newbie questions (read, etc., with Data.ByteString.Lazy.Char8)

Jason Dagit dagit at codersbase.com
Mon Oct 6 22:21:58 EDT 2008

On Mon, Oct 6, 2008 at 7:06 PM, Mike Coleman <tutufan at gmail.com> wrote:

> Hi,
> I could use a little help.  I was looking through the Real World
> Haskell book and came across a trivial program for summing numbers in
> a file.  They mentioned that that implementation was very slow, as
> it's based on String's, so I thought I'd try my hand at converting it
> to use lazy ByteString's.  I've made some progress, but now I'm a
> little stuck because that module doesn't seem to have a 'read' method.
> There's a readInt method, which I guess I could use, but it returns a
> Maybe, and I don't see how I can easily strip that off.
> So:
> 1.  Is there an easy way to strip off the Maybe that would allow an
> equivalently concise definition for sumFile?  I can probably figure
> out how to do it with pattern matching and a separate function--I'm
> just wondering if there's a more concise way.

I'm not a ByteString expert, but there should be an easy way to solve this
issue of Maybe.

If you go to hoogle (http://www.haskell.org/hoogle/) and type this:
[Maybe a] -> [a]
it says:
a] -> [a]<http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:catMaybes>

As the top search result.

This means that you can convert any list of maybes into a list of what you
want.  It just tosses out the Nothings.

> 2.  Why doesn't ByteString implement 'read'?  Is it just that this
> function (like 'input' in Python) isn't really very useful for real
> programs?

I think probably for things more complex than parsing ints it's best to make
your own parser?  I seem to recall that someone was working on a library of
parsing functions based on bytestring?  Maybe someone else can comment?

3.  Why doesn't ByteString implement 'readDouble', etc.?  That is, why
> are Int and Integer treated specially?  Do I not need readDouble?

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.  Once it
was optimized, that must have satisfied the need for fast number reading.

I would agree that at least for Prelude types it would be nice to have
efficient bytestring based parsers.  Do we have Read/Show classes
specifically for working in bytestrings?  Maybe that's what the world needs
in the bytestring api.  Then again, I'm not really qualified to comment :)
For all I know it already exists.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081006/707cb9de/attachment.htm

More information about the Haskell-Cafe mailing list