I experienced a following problem while dealing with some text processing.<br><br>I have a text and want to get the same text with parts enclosed into {} or [] stripped away. Substituting them with a ' ' would also work.<br>
<br>Here is the code I wrote (T is Data.Text):<br><br><span style="font-family: courier new,monospace;">stripBrackets :: T.Text -> T.Text</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">stripBrackets text = snd $ T.mapAccumL f 0 text where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> f depth c = let </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> depth' = depth + d' c</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> c' | depth > 0 || depth' > 0 = ' ' </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> | otherwise = c </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> in </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (depth', c') </span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> d' '{' = 1 </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> d' '[' = 1 </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> d' '}' = -1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> d' ']' = -1</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> d' _ = 0 </span><br><br>The only problem is that it takes about a minute to complete on 3GHz+ processor when text is a 30k chars long.<br><br>Any ideas how to improve this code?<br>
<br>--<br>Regards, Petr<br><br>