break out parseInput - it's good to have dedicated parse stage indicating parsed type.
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