From b3c04ce3d0e0e95a0b3d9be9c58c18426d899d22 Mon Sep 17 00:00:00 2001 From: "Joseph C. Sible" Date: Sun, 5 Apr 2020 15:21:44 -0400 Subject: [PATCH] Implement findFirst in terms of foldr --- src/ShellCheck/AnalyzerLib.hs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/ShellCheck/AnalyzerLib.hs b/src/ShellCheck/AnalyzerLib.hs index 0a4145a..4348438 100644 --- a/src/ShellCheck/AnalyzerLib.hs +++ b/src/ShellCheck/AnalyzerLib.hs @@ -388,14 +388,13 @@ parents params = getPath (parentMap params) -- Find the first match in a list where the predicate is Just True. -- Stops if it's Just False and ignores Nothing. findFirst :: (a -> Maybe Bool) -> [a] -> Maybe a -findFirst p l = - case l of - [] -> Nothing - (x:xs) -> - case p x of - Just True -> return x - Just False -> Nothing - Nothing -> findFirst p xs +findFirst p = foldr go Nothing + where + go x acc = + case p x of + Just True -> return x + Just False -> Nothing + Nothing -> acc -- Check whether a word is entirely output from a single command tokenIsJustCommandOutput t = case t of