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