Re: [Haskell-cafe] Comments on reading two ints off Bytestring

2007-12-24 Thread Conal Elliott
To clean up even more, use StateT B.ByteString Maybe.  Then the ByteString
threading will be invisible, leading to just liftM2 (,) readI readI, for
suitably defined readI.

On Dec 23, 2007 6:45 AM, Bryan O'Sullivan [EMAIL PROTECTED] wrote:

 Paulo J. Matos wrote:

  I guess the latter is the correct guess.

 Good guess!

 You can take advantage of the fact that the Maybe type is an instance of
 the Monad typeclass to chain those computations together, getting rid of
 all of the explicit case analysis.

 import qualified Data.ByteString.Char8 as B
 import Data.Char (isDigit)

 readTwoInts :: B.ByteString - Maybe ((Int, Int), B.ByteString)
 readTwoInts r = do
  (a, s) - B.readInt . B.dropWhile (not . isDigit) $ r
  (b, t) - B.readInt . B.dropWhile (not . isDigit) $ s
  return ((a, b), t)

 Let's try that in ghci:

  *Main readTwoInts (B.pack hello 256 299 remainder)
  Just ((256,299), remainder)

 The case analysis is still happening, it's just being done behind your
 back by the (=) combinator, leaving your code much tidier.  (And why
 is there no explicit use of (=) above?  Read about desugaring of do
 notation in the Haskell 98 report.)

 The learning you'll want to do, to be able to reproduce code such as the
 above, is about monads.

 Cheers,

b
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Comments on reading two ints off Bytestring

2007-12-23 Thread Paulo J. Matos
Hello all,

It is either too difficult to get two integers of a bytestring, in
which case something should be done to ease the process or I should
learn much more Haskell. I guess the latter is the correct guess.

I have a bytestring containing two naturals. I was to get them as
efficiently as possible. Here's my code:
parseHeader :: BS.ByteString - (Int, Int)
parseHeader bs =
let first = BS.readInt $ BS.dropWhile (not . isDigit) bs
in
  if(isNothing first)
  then
  error Couldn't find first natural.
  else
  let second = BS.readInt $ BS.dropWhile (not . isDigit) $
snd $ fromJust first
  in
if(isNothing second)
then
error Couldn't find second natural.
else
(fst $ fromJust first, fst $ fromJust second)

This seems to work:
 parseHeader $ BS.pack hello 252 359
(252,359)

Is there a better way?

Cheers,

-- 
Paulo Jorge Matos - pocm at soton.ac.uk
http://www.personal.soton.ac.uk/pocm
PhD Student @ ECS
University of Southampton, UK
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Comments on reading two ints off Bytestring

2007-12-23 Thread Neil Mitchell
Hi

  parseHeader $ BS.pack hello 252 359
 (252,359)

If this were strings, I'd start with:

map read . words

If you want to have error correction, I'd move to:

mapM readMay . words

(readMay comes from the safe package, http://www-users.cs.york.ac.uk/~ndm/safe/)

I don't know about the bytestring bit, but I guess the functions all map over.

 I have a bytestring containing two naturals. I was to get them as
 efficiently as possible. Here's my code:

Have you profiled your code and found that the parsing of two Int's
from a bytestring is the performance critial bit? If not, I'd just
keep it simple, and then optimise once you know where you should be
optimising.

Thanks

Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Comments on reading two ints off Bytestring

2007-12-23 Thread Bryan O'Sullivan
Paulo J. Matos wrote:

 I guess the latter is the correct guess.

Good guess!

You can take advantage of the fact that the Maybe type is an instance of
the Monad typeclass to chain those computations together, getting rid of
all of the explicit case analysis.

import qualified Data.ByteString.Char8 as B
import Data.Char (isDigit)

readTwoInts :: B.ByteString - Maybe ((Int, Int), B.ByteString)
readTwoInts r = do
  (a, s) - B.readInt . B.dropWhile (not . isDigit) $ r
  (b, t) - B.readInt . B.dropWhile (not . isDigit) $ s
  return ((a, b), t)

Let's try that in ghci:

  *Main readTwoInts (B.pack hello 256 299 remainder)
  Just ((256,299), remainder)

The case analysis is still happening, it's just being done behind your
back by the (=) combinator, leaving your code much tidier.  (And why
is there no explicit use of (=) above?  Read about desugaring of do
notation in the Haskell 98 report.)

The learning you'll want to do, to be able to reproduce code such as the
above, is about monads.

Cheers,

b
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe