[Haskell-cafe] Comments on reading two ints off Bytestring
Paulo J. Matos
pocm at soton.ac.uk
Sun Dec 23 07:32:15 EST 2007
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
error "Couldn't find first natural."
let second = BS.readInt $ BS.dropWhile (not . isDigit) $
snd $ fromJust first
error "Couldn't find second natural."
(fst $ fromJust first, fst $ fromJust second)
This seems to work:
> parseHeader $ BS.pack "hello 252 359"
Is there a better way?
Paulo Jorge Matos - pocm at soton.ac.uk
PhD Student @ ECS
University of Southampton, UK
More information about the Haskell-Cafe