[Haskell-cafe] attoparsec and vectors

Eric Rasmussen ericrasmussen at gmail.com
Wed Jun 29 00:20:46 CEST 2011


Hi everyone,

I have a task that involves parsing a flat-file into a vector from the
Data.Vector package. In the interest of keeping things simple, I first used
Attoparsec.Text's version of "many" and then converted the resulting list to
a vector:

import qualified Data.Vector as V
import Data.Attoparsec.Text as A
import Control.Applicative

getData = do results <- A.many someParser
    return (V.fromList results)

It runs quickly, but I naively thought I could outperform it by reworking
"many" to build a vector directly, instead of having to build a list first
and then convert it to a vector:

manyVec :: Alternative f => f a -> f (V.Vector a)
manyVec v = many_v
  where many_v = some_v <|> pure V.empty
        some_v = V.cons <$> v <*> many_v

Unfortunately, manyVec either quickly leads to a stack space overflow, or it
takes an enormous amount of time to run. Does anyone know of a better way to
build up a vector from parsing results?

Thanks for any tips or insight,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110628/ca617bbe/attachment.htm>


More information about the Haskell-Cafe mailing list