Created
October 14, 2015 09:59
-
-
Save vlas-ilya/3c359539317db86b2e80 to your computer and use it in GitHub Desktop.
parsePerson "firstName = John\nlastName = Connor\nage = 30"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Char(isDigit) | |
data Error = ParsingError | IncompleteDataError | IncorrectDataError String | |
deriving (Show) | |
data Person = Person { firstName :: String, lastName :: String, age :: Int } | |
deriving (Show) | |
split delimiter = foldr f [[]] | |
where f c l@(x:xs) | c == delimiter = []:l | |
| otherwise = (c:x):xs | |
parsePerson :: String -> Either Error Person | |
parsePerson str | |
| notAllFields = Left IncompleteDataError | |
| incompleteDataError' = Left ParsingError | |
| incompleteDataError = Left ParsingError | |
| not ageCorrect = Left (IncorrectDataError (age'')) | |
| otherwise = Right (Person firstName'' lastName'' (read age'' :: Int)) | |
where | |
fields = (split '\n' str) | |
notAllFields = length (split '\n' str) < 3 | |
incompleteDataError' = length (split '=' (fields !! 0)) /= 2 || | |
length (split '=' (fields !! 1)) /= 2 || | |
length (split '=' (fields !! 2)) /= 2 | |
[fnName, firstName'] = split '=' (fields !! 0) | |
[lnName, lastName'] = split '=' (fields !! 1) | |
[aName, age'] = split '=' (fields !! 2) | |
incompleteDataError = not (init fnName == "firstName" && | |
init lnName == "lastName" && | |
init aName == "age") | |
age'' = tail age' | |
ageCorrect = all (\x -> isDigit x) age'' | |
firstName'' = tail firstName' | |
lastName'' = tail lastName' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment