Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Last active May 8, 2016 03:54
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 TGOlson/58e413f18b02db20cfbe84e6191a1758 to your computer and use it in GitHub Desktop.
Save TGOlson/58e413f18b02db20cfbe84e6191a1758 to your computer and use it in GitHub Desktop.
Nested HashMap implementation.
module NestedHashMap where
import Control.Lens
import Data.Hashable (Hashable)
import Data.HashMap.Strict (HashMap, empty)
type NestedHashMap a b c = HashMap a (HashMap b c)
lookup
:: (Eq a, Hashable a, Eq b, Hashable b, Eq c)
=> a -> b -> NestedHashMap a b c -> Maybe c
lookup k k' hash = hash ^.at k . non empty ^.at k'
-- Insert a value. Creating top level key if one does not exist.
insert
:: (Eq a, Hashable a, Eq b, Hashable b, Eq c)
=> a -> b -> c -> NestedHashMap a b c -> NestedHashMap a b c
insert k k' v hash = hash & at k . non empty . at k' ?~ v
-- Delete a key. Deleting top level key if it is empty after delete.
delete
:: (Eq a, Hashable a, Eq b, Hashable b, Eq c)
=> a -> b -> NestedHashMap a b c -> NestedHashMap a b c
delete k k' hash = hash & at k . non empty . at k' .~ Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment