Skip to content

Instantly share code, notes, and snippets.

@eXeC64
Last active August 29, 2015 14:11
Show Gist options
  • Save eXeC64/be831c7b4cc785ba09aa to your computer and use it in GitHub Desktop.
Save eXeC64/be831c7b4cc785ba09aa to your computer and use it in GitHub Desktop.
CSV x,y -> y,count(y)
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