Made code more monadic

This commit is contained in:
Anton Vakhrushev 2020-06-20 19:56:12 +03:00
parent 2e6e06310d
commit 5cf3aa8fba

View File

@ -21,19 +21,14 @@ data Pattern =
deriving (Show) deriving (Show)
match :: String -> DateTime -> Maybe Bool match :: String -> DateTime -> Maybe Bool
match s d = match ptrn datetime = parse ptrn >>= \p -> Just (check p datetime)
case parse s of
Just p -> Just (check p d)
Nothing -> Nothing
parse :: String -> Maybe Pattern parse :: String -> Maybe Pattern
parse text parse text = do
| isValid = Just (createPattern $ catMaybes fields) xs <- sequence $ zipWith parseField (words text) constraints
| otherwise = Nothing if length xs /= 5
where then Nothing
fields = zipWith parseField (words text) constraints else Just $
isValid = checkFields fields
createPattern xs =
Pattern Pattern
{ cminute = head xs { cminute = head xs
, chour = xs !! 1 , chour = xs !! 1
@ -42,12 +37,6 @@ parse text
, cweek = xs !! 4 , cweek = xs !! 4
} }
checkFields :: [Maybe Field] -> Bool
checkFields xs
| length xs /= 5 = False
| any isNothing xs = False
| otherwise = True
constraints :: [Constraint] constraints :: [Constraint]
constraints = constraints =
[ Constraint 0 59 [ Constraint 0 59