From a14bdf1f5325c7ec9da9f8eb2bf6aff487a155d9 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 12 Nov 2017 11:07:42 +0300 Subject: [PATCH] Fix linting hints --- app/Main.hs | 14 +++++++------- src/Constraint.hs | 2 +- src/Field.hs | 15 +++++---------- src/Pattern.hs | 30 +++++++++++++++--------------- test/ConstraintSpec.hs | 15 +++++++-------- test/HelperSpec.hs | 19 +++++++++---------- test/PatternSpec.hs | 18 ++++++++---------- 7 files changed, 52 insertions(+), 61 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a9358b5..d2ac942 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -11,16 +11,16 @@ main :: IO () main = do args <- getArgs dt <- getCurrentDateTime - case processArgs args dt of - Just True -> exitWith ExitSuccess - Just False -> exitWith (ExitFailure 1) - Nothing -> exitWith (ExitFailure 2) + exitWith $ case processArgs args dt of + Just True -> ExitSuccess + Just False -> ExitFailure 1 + Nothing -> ExitFailure 2 processArgs :: [String] -> DateTime -> Maybe Bool -processArgs [pattern] dt = safeMatch pattern dt -processArgs [pattern, time] dt = matchGivenTime pattern (parseDate dt time) +processArgs [ptn] dt = safeMatch ptn dt +processArgs [ptn, time] dt = matchGivenTime ptn (parseDate dt time) processArgs _ _ = Nothing matchGivenTime :: String -> Either ParseError DateTime -> Maybe Bool matchGivenTime _ (Left _) = Nothing -matchGivenTime pattern (Right dt) = safeMatch pattern dt +matchGivenTime ptn (Right dt) = safeMatch ptn dt diff --git a/src/Constraint.hs b/src/Constraint.hs index 8556617..d20ac87 100644 --- a/src/Constraint.hs +++ b/src/Constraint.hs @@ -12,4 +12,4 @@ inside :: (Int, Int) -> Constraint -> Bool inside (x, y) (Constraint lower upper) = x >= lower && y <= upper inRange :: Int -> Constraint -> Bool -inRange x cons = inside (x, x) cons +inRange x = inside (x, x) diff --git a/src/Field.hs b/src/Field.hs index 41c3287..f54a71f 100644 --- a/src/Field.hs +++ b/src/Field.hs @@ -16,7 +16,7 @@ data Field = Field Range Step deriving (Eq, Show) parseField :: String -> Constraint -> Maybe Field -parseField text constraint = parseField' (wordsWhen (== '/') text) constraint +parseField text = parseField' (wordsWhen (== '/') text) parseField' :: [String] -> Constraint -> Maybe Field parseField' [rangeText] constraint @@ -34,20 +34,15 @@ parseField' _ _ = Nothing parseFieldRange :: String -> Constraint -> Maybe Range parseFieldRange text constraint - | isAll = Just All + | text == "*" = Just All | isJust number = Just (Range (fromJust number) (fromJust number)) - | isJust range = Just (Range (fst $ fromJust range) (snd $ fromJust range)) + | isJust range = Just (uncurry Range (fromJust range)) | isJust sequence = fmap Sequence sequence where - isAll = parseAll text number = parseNumber text constraint range = parseRange text constraint sequence = parseSequence text constraint -parseAll :: String -> Bool -parseAll "*" = True -parseAll _ = False - isNumber :: String -> Bool isNumber = all isDigit @@ -67,7 +62,7 @@ parseRange text constraint pieces = wordsWhen (== '-') text isTwo = length pieces == 2 isAllNumbers = all isNumber pieces - start = read (pieces !! 0) :: Int + start = read (head pieces) :: Int end = read (pieces !! 1) :: Int isValid = isTwo && isAllNumbers && start <= start && (start, end) `inside` constraint @@ -79,7 +74,7 @@ parseSequence text constraint pieces = wordsWhen (== ',') text isAllNumbers = all isNumber pieces numbers = map read pieces - allInRange = all (\x -> x `inRange` constraint) numbers + allInRange = all (`inRange` constraint) numbers isValid = length pieces >= 2 && isAllNumbers && allInRange parseFieldStep :: String -> Maybe Step diff --git a/src/Pattern.hs b/src/Pattern.hs index d250b64..509232c 100644 --- a/src/Pattern.hs +++ b/src/Pattern.hs @@ -39,9 +39,9 @@ parse s | otherwise = Just (createPattern $ catMaybes parts) where parts = createParts s - isInvalid = checkParts parts == False + isInvalid = not (checkParts parts) createPattern xs = Pattern { - cminute = xs !! 0, + cminute = head xs, chour = xs !! 1, cday = xs !! 2, cmonth = xs !! 3, @@ -49,9 +49,9 @@ parse s cyear = xs !! 5 } -createParts s = map f $ zip parsers (words s) - where - f (g, s) = g s +createParts s = zipWith (curry f) parsers (words s) + where + f (g, s) = g s checkParts :: [Maybe Field] -> Bool checkParts xs @@ -72,13 +72,13 @@ parseYear = parseFieldAdapter (Constraint 0 9999) parsers = [parseMinute, parseHour, parseDay, parseMonth, parseWeek, parseYear] check :: Pattern -> DateTime -> Bool -check pattern date = all isRight pairs - where - pairs = [ (cminute pattern, minute date), - (chour pattern, hour date), - (cday pattern, day date), - (cmonth pattern, month date), - (cweek pattern, weekdayNumber $ dateWeekDay date), - (cyear pattern, year date) - ] - isRight (pattern, value) = matchField pattern value +check ptn date = all isRight pairs + where + pairs = [ (cminute ptn, minute date), + (chour ptn, hour date), + (cday ptn, day date), + (cmonth ptn, month date), + (cweek ptn, weekdayNumber $ dateWeekDay date), + (cyear ptn, year date) + ] + isRight (p, value) = matchField p value diff --git a/test/ConstraintSpec.hs b/test/ConstraintSpec.hs index 2795624..44125db 100644 --- a/test/ConstraintSpec.hs +++ b/test/ConstraintSpec.hs @@ -8,13 +8,12 @@ main :: IO () main = hspec spec spec :: Spec -spec = do - describe "Constraint" $ do - it "can be created from number" $ - makeRangeFromNumber 10 `shouldBe` Constraint 10 10 +spec = describe "Constraint" $ do + it "can be created from number" $ + makeRangeFromNumber 10 `shouldBe` Constraint 10 10 - it "validate number" $ - 10 `inRange` (Constraint 0 10) `shouldBe` True + it "validate number" $ + 10 `inRange` Constraint 0 10 `shouldBe` True - it "validate number" $ - 10 `inRange` (Constraint 15 20) `shouldBe` False + it "validate number" $ + 10 `inRange` Constraint 15 20 `shouldBe` False diff --git a/test/HelperSpec.hs b/test/HelperSpec.hs index 25818df..7c4e16e 100644 --- a/test/HelperSpec.hs +++ b/test/HelperSpec.hs @@ -7,16 +7,15 @@ main :: IO () main = hspec spec spec :: Spec -spec = do - describe "Splitting" $ do - it "can process empty string" $ - wordsWhen (== '-') "" `shouldBe` [] +spec = describe "Splitting" $ do + it "can process empty string" $ + wordsWhen (== '-') "" `shouldBe` [] - it "can process only one word" $ - wordsWhen (== '-') "10" `shouldBe` ["10"] + it "can process only one word" $ + wordsWhen (== '-') "10" `shouldBe` ["10"] - it "can separated by '-'" $ - wordsWhen (== '-') "10-20" `shouldBe` ["10", "20"] + it "can separated by '-'" $ + wordsWhen (== '-') "10-20" `shouldBe` ["10", "20"] - it "can be separated by ','" $ - wordsWhen (== ',') "10,20,30" `shouldBe` ["10", "20", "30"] + it "can be separated by ','" $ + wordsWhen (== ',') "10,20,30" `shouldBe` ["10", "20", "30"] diff --git a/test/PatternSpec.hs b/test/PatternSpec.hs index c8097c3..f80d9e0 100644 --- a/test/PatternSpec.hs +++ b/test/PatternSpec.hs @@ -17,29 +17,27 @@ spec = do it "matches fixed time" $ let - pattern = "* * * * * *" + ptn = "* * * * * *" date = DateTime 2017 10 11 0 0 0 in - match pattern date `shouldBe` True + match ptn date `shouldBe` True it "matches all minutes" $ let - pattern = "* * * * * *" + ptn = "* * * * * *" dates = [DateTime 2017 10 11 0 i 0 | i <- [0..59]] in - countMatches pattern dates `shouldBe` 60 + countMatches ptn dates `shouldBe` 60 it "matches exactly moment" $ let - date = (DateTime 2017 10 11 0 0 0) - pattern = "0 0 11 10 * 2017" + date = DateTime 2017 10 11 0 0 0 + ptn = "0 0 11 10 * 2017" in - match pattern date `shouldBe` True + match ptn date `shouldBe` True countMatches :: String -> [DateTime] -> Int countMatches p xs = sum $ map (f p) xs where - f x d = case match x d of - True -> 1 - False -> 0 + f x d = if match x d then 1 else 0