From bd116f252b1dba4d07f3a10994fc00529158ad91 Mon Sep 17 00:00:00 2001 From: "Joseph C. Sible" Date: Sat, 8 Feb 2020 22:55:45 -0500 Subject: [PATCH] Use findM instead of filterM Using filterM makes us run the monadic predicate on every list element. Use findM instead so that we can stop once we find a matching one. --- shellcheck.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/shellcheck.hs b/shellcheck.hs index 20fb4b6..f1757ef 100644 --- a/shellcheck.hs +++ b/shellcheck.hs @@ -491,6 +491,12 @@ ioInterface options files = do first <- a arg if not first then return False else b arg + findM p = foldr go (pure Nothing) + where + go x acc = do + b <- p x + if b then pure (Just x) else acc + findSourceFile inputs sourcePathFlag currentScript sourcePathAnnotation original = if isAbsolute original then @@ -500,11 +506,11 @@ ioInterface options files = do find original original where find filename deflt = do - sources <- filterM ((allowable inputs) `andM` doesFileExist) $ + sources <- findM ((allowable inputs) `andM` doesFileExist) $ (adjustPath filename):(map ( filename) $ map adjustPath $ sourcePathFlag ++ sourcePathAnnotation) case sources of - [] -> return deflt - (first:_) -> return first + Nothing -> return deflt + Just first -> return first scriptdir = dropFileName currentScript adjustPath str = case (splitDirectories str) of