Last active
August 29, 2015 14:11
-
-
Save eXeC64/be831c7b4cc785ba09aa to your computer and use it in GitHub Desktop.
CSV x,y -> y,count(y)
This file contains hidden or 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 qualified Data.Map.Strict as Map | |
import Data.Maybe (mapMaybe) | |
import Text.CSV (parseCSV) | |
import Text.Printf (printf) | |
parseFilm (t:y:[]) = Just (t,y) | |
parseFilm _ = Nothing | |
countYears :: [(String,String)] -> [(String,Int)] | |
countYears films = Map.toList $ foldl (\m (_,y) -> Map.insertWith (+) y 1 m) Map.empty films | |
processFilms :: String -> String | |
processFilms s = unlines . map (uncurry $ printf "%s,%d") $ countYears films | |
where films = either (const []) (mapMaybe parseFilm) $ parseCSV "stdin" s | |
main = interact processFilms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment