Skip to content

Instantly share code, notes, and snippets.

@nbogie
Created March 5, 2011 15:11
Show Gist options
  • Save nbogie/856422 to your computer and use it in GitHub Desktop.
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.
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