Created
March 5, 2011 15:11
-
-
Save nbogie/856422 to your computer and use it in GitHub Desktop.
break out parseInput - it's good to have dedicated parse stage indicating parsed type.
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.Function (on) | |
import Data.List (groupBy) | |
import Data.Maybe (mapMaybe) | |
import qualified Data.List.Utils as U | |
type Domain = String | |
type NameServer = String | |
main :: IO () | |
main = interact process | |
where | |
process :: String -> String | |
process = unlines . map show . parseInput | |
parseInput :: String -> [(Domain, [NameServer])] | |
parseInput input = map squish groupedPairs | |
where | |
groupedPairs = groupBy ((==) `on` fst) pairs | |
pairs = mapMaybe parseLine (lines input) | |
parseLine :: String -> Maybe (Domain, NameServer) | |
parseLine line = case U.split "ORG" line of | |
(d:n:[]) -> Just (d, n) | |
_ -> Nothing | |
-- create (domain, [ns1, ns2, ns3]) tuples | |
squish :: [(Domain, NameServer)] -> (Domain, [NameServer]) | |
squish [] = error "BUG: empty domain group given to squish!" | |
squish full@((d,ns1) : _) = (d, map snd full) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment