Hi everyone,<br><br>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:<br>
<br><span style="font-family: courier new,monospace;">import qualified Data.Vector as V</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import Data.Attoparsec.Text as A</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import Control.Applicative</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">getData = do results <- A.many someParser</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return (V.fromList results)</span><br><br>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:<br>
<br><span style="font-family: courier new,monospace;">manyVec :: Alternative f => f a -> f (V.Vector a)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
manyVec v = many_v</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
where many_v = some_v <|> pure V.empty</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
some_v = V.cons <$> v <*> many_v</span><br style="font-family: courier new,monospace;">
<br>
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? <br><br>Thanks for any tips or insight,<br>
Eric<br><br>