Skip to content

Instantly share code, notes, and snippets.

@portnov
Created July 31, 2018 12:06
Show Gist options
  • Save portnov/6615d9636f91acce3417c7e9d7ab4622 to your computer and use it in GitHub Desktop.
Save portnov/6615d9636f91acce3417c7e9d7ab4622 to your computer and use it in GitHub Desktop.
Data.Map
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Monoid
type Classification = M.Map String String
type Value = Int
src :: M.Map Classification Value
src =
let key1 = M.fromList [("cls1", "v1"), ("cls2", "v2")]
key2 = M.fromList [("cls1", "v1"), ("cls2", "v3")]
key3 = M.fromList [("cls1", "v4"), ("cls2", "v2")]
in M.fromList [(key1, 5), (key2, 10), (key3, 7)]
main :: IO ()
main = do
let keys = S.fromList ["cls1"]
let result = M.mapKeysWith (+) (\cls -> M.restrictKeys cls keys) src
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment