0

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 ?

vkubicki
  • 1,104
  • 1
  • 11
  • 26

1 Answers1

0

You can use singleton c instead of pack [c]. But beyond that, I don't see any obvious improvement.

This is fine if mconcat is used sparsely. If you need to append a lot of Text together, you should use a Builder instead.

Li-yao Xia
  • 31,896
  • 2
  • 33
  • 56