Last active
May 8, 2016 03:54
-
-
Save TGOlson/58e413f18b02db20cfbe84e6191a1758 to your computer and use it in GitHub Desktop.
Nested HashMap implementation.
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
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