From 06661e173ef7a6b6d3fe2ae6f9c4fc30322f3687 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Tue, 14 Nov 2017 20:55:58 +0300 Subject: [PATCH] More simplify code --- src/Pattern.hs | 23 +++++++---------------- test/PatternSpec.hs | 1 - 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Pattern.hs b/src/Pattern.hs index 450a5c5..3ac8f1b 100644 --- a/src/Pattern.hs +++ b/src/Pattern.hs @@ -3,7 +3,6 @@ module Pattern , match , parse , check - , createFields ) where import Constraint @@ -27,12 +26,12 @@ match s d = Nothing -> Nothing parse :: String -> Maybe Pattern -parse s - | isInvalid = Nothing - | otherwise = Just (createPattern $ catMaybes parts) +parse text + | isValid = Just (createPattern $ catMaybes fields) + | otherwise = Nothing where - parts = createFields s - isInvalid = not (checkParts parts) + fields = zipWith parseField (words text) constraints + isValid = checkFields fields createPattern xs = Pattern { cminute = head xs @@ -43,20 +42,12 @@ parse s , cyear = xs !! 5 } -createFields :: String -> [Maybe Field] -createFields text = zipWith f constraints (words text) - where - f constraint word = parseField word constraint - -checkParts :: [Maybe Field] -> Bool -checkParts xs +checkFields :: [Maybe Field] -> Bool +checkFields xs | length xs /= 6 = False | any isNothing xs = False | otherwise = True -parseFieldAdapter :: Constraint -> String -> Maybe Field -parseFieldAdapter constraint text = parseField text constraint - constrainMinute :: Constraint constrainMinute = Constraint 0 59 diff --git a/test/PatternSpec.hs b/test/PatternSpec.hs index 6ab58cc..a0de050 100644 --- a/test/PatternSpec.hs +++ b/test/PatternSpec.hs @@ -14,7 +14,6 @@ main = hspec spec spec :: Spec spec = describe "Cron pattern" $ do - it "createFields" $ length (createFields "* * * * * *") `shouldBe` 6 it "matches fixed time" $ let ptn = "* * * * * *" date = DateTime 2017 10 11 0 0 0