Hi all,

Using pandoc-types' Walkable class, what is the best-performing/most idiomatic way to filter out certain elements from a document?

Say I wanted to remove all occurrences of the word "the" from a Pandoc. The best implementation I've been able to write for this is

isThe :: Inline -> Bool
isThe (Str "the") = True
isThe _ = False

removeThe :: Pandoc -> Pandoc
removeThe = walk (filter $ not . isThe)

Is this right? Does the use of filter here not mean an additional O(n) traversal happens on every list of Inlines?

I feel like a better solution for this would be using

filterThe :: Inline -> [Inline]
filterThe (Str "the") = []
filterThe x = [x]

or something similar, but of course

removeThe = walk filterThe

does not typecheck. We need a -> a, meaning [Inline] -> [Inline] here.


The same question actually applies to any transformation which "changes the number of elements":

theFine :: Inline -> [Inline]
theFine (Str "the") = [Str "the", Space, Str "fine"]
theFine i = [i]

allIsFine :: Pandoc -> Pandoc
allIsFine = walk $ concatMap theFine

Best,

Ilia

P.S. Sorry if this has been asked before. I feel it must be a common issue, but all I've been able to find is this thread with its links, which are 7 years old now and seem to be calling for changes in pandoc. Everything else suggests stepping outside Haskell.

--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/915213fc-e4c4-480b-a6a2-fd3420777ddan%40googlegroups.com.