Skip to content

Instantly share code, notes, and snippets.

@jhickner
Created October 30, 2013 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhickner/7224885 to your computer and use it in GitHub Desktop.
Save jhickner/7224885 to your computer and use it in GitHub Desktop.
import qualified Data.Map as M
type Name = String
data Action = MadeShot | MissedShot | Rebound deriving (Show, Eq)
data Event = Event Name Action
type Players = M.Map Name (Int, Int, Int)
aggregate :: [Event] -> Players
aggregate = M.fromListWith f . map toPair
where
f (a, b, c) (a', b', c') = (a+a', b+b', c+c')
toPair (Event name action) = case action of
MadeShot -> (name, (1, 0, 0))
MissedShot -> (name, (0, 1, 0))
Rebound -> (name, (0, 0, 1))
events = [Event "playera" MadeShot, Event "playera" MadeShot, Event "playerb" Rebound]
{-
> aggregate events
fromList [("playera",(2,0,0)),("playerb",(0,0,1))]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment