Made code more monadic
This commit is contained in:
parent
2e6e06310d
commit
5cf3aa8fba
@ -21,32 +21,21 @@ 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
|
Pattern
|
||||||
createPattern xs =
|
{ cminute = head xs
|
||||||
Pattern
|
, chour = xs !! 1
|
||||||
{ cminute = head xs
|
, cday = xs !! 2
|
||||||
, chour = xs !! 1
|
, cmonth = xs !! 3
|
||||||
, cday = xs !! 2
|
, cweek = xs !! 4
|
||||||
, cmonth = xs !! 3
|
}
|
||||||
, 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 =
|
||||||
|
Loading…
Reference in New Issue
Block a user