Hacker News new | ask | show | jobs
by codygman 4120 days ago
For whatever reason I felt the random need to parse your table here in Haskell, so.... here's the code :P

    λ> import qualified Data.Text.IO as T
    λ> import qualified Data.Text as T
    λ> data Entry = Entry {name :: T.Text,  macroPerf :: Int, charLength :: Int} deriving Show
    λ> -- parse into list of lists
    λ> let table = (fmap T.words) . T.lines <$> T.readFile "langstats.txt"
    λ> -- I need to parse these things... safely.. using the Safe package!
    λ> import Safe
    λ> -- parse table into list of `Maybe Entry` taking advantage of applicative instance
    λ> -- the list of lists are just name, perf3 macros, chars, and a field I'm ignoring for now
    λ> -- we can just use list destructuring (\(name : perf3macros : chars : _))
    λ> entries <- fmap (\(n:p3:chars:_) -> liftA3 Entry (Just n) (toInt p3) (toInt chars)) <$> table
    λ> :t entries
    entries :: [Maybe Entry]
    λ> -- for easy sorting, we want just [Entry]
    λ> -- safely turn our [Maybe Entry] into [Entry]
    λ> import Data.Maybe (isJust, fromJust)
    λ> let entries' = map fromJust . filter isJust $ entries -- safe because we filter out the justs first!
    λ> -- speaking of sorting... we need a couple imports
    λ> import Data.List (sortBy)
    λ> import Data.Function (on)
    λ> let results = sortBy (compare `on` macroPerf) entries'
    [Entry {name = "python", macroPerf = 304, charLength = 19632},Entry {name = "php", macroPerf = 331, charLength = 27332},Entry {name = "forth", macroPerf = 563, charLength = 44715},Entry {name = "haskell", macroPerf = 1163, charLength = 30115},Entry {name = "clojure", macroPerf = 1174, charLength = 10099},Entry {name = "ruby", macroPerf = 1255, charLength = 13247},Entry {name = "js", macroPerf = 1726, charLength = 26437},Entry {name = "coffee", macroPerf = 2326, charLength = 15653},Entry {name = "racket", macroPerf = 2461, charLength = 17229},Entry {name = "go", macroPerf = 3048, charLength = 36321},Entry {name = "c", macroPerf = 3649, charLength = 73047},Entry {name = "rust", macroPerf = 4084, charLength = 56516},Entry {name = "vb", macroPerf = 4523, charLength = 58099},Entry {name = "cs", macroPerf = 5414, charLength = 45039},Entry {name = "ocaml", macroPerf = 7063, charLength = 24371},Entry {name = "nim", macroPerf = 11121, charLength = 20263},Entry {name = "scala", macroPerf = 15963, charLength = 24885},Entry {name = "java", macroPerf = 17969, charLength = 53223}]
    λ> -- how about some pretty printing?
    λ> mapM_ (\e -> putStrLn $ (T.unpack (name e)) ++ "\t" ++ show (macroPerf e) ++ "\t" ++ show (charLength e)) results