This module exists to provide documentation for lenses for working with
HashMap, which might otherwise be obscured by their genericity.
HashMap is an instance of At and provides
at as a lens on values at keys:
Example1 expression>>> HashMap.fromList [(1, "world")] ^. at 1Just "world"
Example1 expression>>> HashMap.empty & at 1 .~ Just "world"fromList [(1,"world")]
Example1 expression>>> HashMap.empty & at 0 .~ Just "hello"fromList [(0,"hello")]
We can traverse, fold over, and map over key-value pairs in a HashMap,
thanks to indexed traversals, folds and setters.
Example1 expression>>> iover imapped const $ HashMap.fromList [(1, "Venus")]fromList [(1,1)]
Example1 expression>>> ifoldMapOf ifolded (\i _ -> Sum i) $ HashMap.fromList [(2, "Earth"), (3, "Mars")]Sum {getSum = 5}Example1 expression>>> itraverseOf_ ifolded (curry print) $ HashMap.fromList [(4, "Jupiter")](4,"Jupiter")
Example1 expression>>> itoListOf ifolded $ HashMap.fromList [(5, "Saturn")][(5,"Saturn")]
A related class, Ixed, allows us to use ix
to traverse a value at a particular key.
Example1 expression>>> HashMap.fromList [(2, "Earth")] & ix 2 %~ ("New " ++)fromList [(2,"New Earth")]Example1 expression>>> preview (ix 8) HashMap.emptyNothing