aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Shared.hs
diff options
context:
space:
mode:
authorJoseph C. Sible <josephcsible@users.noreply.github.com>2020-02-08 12:12:30 -0500
committerGitHub <noreply@github.com>2020-02-08 09:12:30 -0800
commitf2f559003e4fe40c3623d1b7acfbeea5a8da9f34 (patch)
tree0dff787cd84437eb8ddd5396187920a72804c48e /src/Text/Pandoc/Shared.hs
parent12c75701be4b87fc438be68af5658d71e5578f62 (diff)
downloadpandoc-f2f559003e4fe40c3623d1b7acfbeea5a8da9f34.tar.gz
Factor out a findM function (#6125)
This adds a new function to the API: Text.Pandoc.Shared.findM.
Diffstat (limited to 'src/Text/Pandoc/Shared.hs')
-rw-r--r--src/Text/Pandoc/Shared.hs9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index a0465211a..cc0808915 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -27,6 +27,7 @@ module Text.Pandoc.Shared (
splitTextByIndices,
substitute,
ordNub,
+ findM,
-- * Text processing
ToString (..),
ToText (..),
@@ -198,6 +199,14 @@ ordNub l = go Set.empty l
go s (x:xs) = if x `Set.member` s then go s xs
else x : go (Set.insert x s) xs
+findM :: forall m t a. (Monad m, Foldable t) => (a -> m Bool) -> t a -> m (Maybe a)
+findM p = foldr go (pure Nothing)
+ where
+ go :: a -> m (Maybe a) -> m (Maybe a)
+ go x acc = do
+ b <- p x
+ if b then pure (Just x) else acc
+
--
-- Text processing
--