Moduleoptics-core-0.4.1.1Haskell2010
Data.IntMap.Optics
IntMap is an instance of At and provides
at as a lens on values at keys:
Example1 expression>>> IntMap.fromList [(1, "world")] ^. at 1Just "world"
Example1 expression>>> IntMap.empty & at 1 .~ Just "world"fromList [(1,"world")]
Example1 expression>>> IntMap.empty & at 0 .~ Just "hello"fromList [(0,"hello")]
We can traverse, fold over, and map over key-value pairs in an IntMap,
thanks to indexed traversals, folds and setters.
Example1 expression>>> iover imapped const $ IntMap.fromList [(1, "Venus")]fromList [(1,1)]
Example1 expression>>> ifoldMapOf ifolded (\i _ -> Sum i) $ IntMap.fromList [(2, "Earth"), (3, "Mars")]Sum {getSum = 5}Example1 expression>>> itraverseOf_ ifolded (curry print) $ IntMap.fromList [(4, "Jupiter")](4,"Jupiter")
Example1 expression>>> itoListOf ifolded $ IntMap.fromList [(5, "Saturn")][(5,"Saturn")]
A related class, Ixed, allows us to use ix to
traverse a value at a particular key.
Example1 expression>>> IntMap.fromList [(2, "Earth")] & ix 2 %~ ("New " ++)fromList [(2,"New Earth")]Example1 expression>>> preview (ix 8) IntMap.emptyNothing