This is similar to my previous question Attoparsec efficient parser for multiple char, but I oversimplified the example parser I provided. I really apologize if it is considered spamming.
If I define
charToText :: Char -> Text
charToText c = pack [c]
parseEqStarMonad :: Parser Text
-- I will not define it here, but it could be any Parser Text
envParser :: Parser Text
envParser = mconcat <$> many (parseEqStarMonad <|> (charToText <$> anyChar))
it seems to me that lifting charToText is inefficient, because for each character matched charToText creates a singleton list to pack it as a Text.
Is there or more efficient way to perform this parsing ?
You can use
singleton cinstead ofpack [c]. But beyond that, I don't see any obvious improvement.This is fine if
mconcatis used sparsely. If you need to append a lot ofTexttogether, you should use aBuilderinstead.