Created
October 30, 2013 00:01
-
-
Save jhickner/7224885 to your computer and use it in GitHub Desktop.
This file contains 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 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