Skip to content

Instantly share code, notes, and snippets.

@nbogie
Created March 5, 2011 14:48
Show Gist options
  • Save nbogie/856404 to your computer and use it in GitHub Desktop.
Save nbogie/856404 to your computer and use it in GitHub Desktop.
Still avoiding bytestring, and without anticipating next stage. Perf ok: 1 million lines in 2 sec and 1 Mb ram, with -O2
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 (processLines . lines)
-- note that there's no IO taint here. much better.
processLines :: [String] -> String
processLines ls = unlines $ map (show . squish) groupedPairs
where
groupedPairs = groupBy ((==) `on` fst) pairs
pairs = mapMaybe parseLine ls
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