Fix linting hints

This commit is contained in:
Anton Vakhrushev 2017-11-12 11:07:42 +03:00
parent 3244ce81f3
commit a14bdf1f53
7 changed files with 52 additions and 61 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"]

View File

@ -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