Need an efficient way to turn (Seq Data.Text) into Data.Text

I have a program (SAX parser using Text.XML.Expat.SAX ) that creates very large CDATA nodes using repetitive additions of Data.Text content using Data.Sequence.(|>) follows:

 existingText |> newTextChunk 

This creates a very large portion of Seq Text type Seq Text .

After I created the data, I need to convert Seq Text -> Text . But this solution I tried was super-slow:

 Data.Foldable.foldr1 Data.Text.append seqText 

Is there a faster way to turn a sequence of text into a regular text binding?

Another way to ask this may be that the most efficient way is to merge a list of text into one text, i.e. [Text] -> Text ?

+4
source share
1 answer

append will create a new array for each item in the list and copy all the data into it. As one of the comments said, you can try concat . For consistency, you can try:

 import Data.Foldable (toList) import Data.Sequence (Seq) import qualified Data.Sequence as S import Data.Text (Text) import qualified Data.Text as T concatSeq :: Seq Text -> Text concatSeq = T.concat . toList 

This should be faster than doing fold with the append , but I have not tested it. You can try to hack a small test case using criteria (which is an awesome library).

+7
source

Source: https://habr.com/ru/post/1486297/


All Articles