HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

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
  • 5 values
valuetoMapOf
  1. :: (Is k A_Fold, HasSingleIndex is Int)
  2. => Optic' k is s a
  3. -> s
  4. -> IntMap a
#

Construct a map from an IxFold.

The construction is left-biased (see union), i.e. the first occurrences of keys in the fold or traversal order are preferred.

Example1 expression
toMapOf ifolded ["hello", "world"]fromList [(0,"hello"),(1,"world")]
Example1 expression
toMapOf (folded % ifolded) [(1,"alpha"),(2, "beta")]fromList [(1,"alpha"),(2,"beta")]
Example1 expression
toMapOf (icompose (\a b -> 10*a+b) $ ifolded % ifolded) ["foo", "bar"]fromList [(0,'f'),(1,'o'),(2,'o'),(10,'b'),(11,'a'),(12,'r')]
Example1 expression
toMapOf (folded % ifolded) [(1, "hello"), (2, "world"), (1, "dummy")]fromList [(1,"hello"),(2,"world")]
valuelt :: Int -> IxAffineTraversal' Int (IntMap v) v
#

Focus on the largest key smaller than the given one and its corresponding value.

Example1 expression
IntMap.fromList [(1, "hi"), (2, "there")] & over (lt 2) (++ "!")fromList [(1,"hi!"),(2,"there")]
Example1 expression
ipreview (lt 1) $ IntMap.fromList [(1, 'x'), (2, 'y')]Nothing
valuegt :: Int -> IxAffineTraversal' Int (IntMap v) v
#

Focus on the smallest key greater than the given one and its corresponding value.

Example1 expression
IntMap.fromList [(1, "hi"), (2, "there")] & over (gt 2) (++ "!")fromList [(1,"hi"),(2,"there")]
Example1 expression
ipreview (gt 1) $ IntMap.fromList [(1, 'x'), (2, 'y')]Just (2,'y')
valuele :: Int -> IxAffineTraversal' Int (IntMap v) v
#

Focus on the largest key smaller or equal than the given one and its corresponding value.

Example1 expression
IntMap.fromList [(1, "hi"), (2, "there")] & over (le 2) (++ "!")fromList [(1,"hi"),(2,"there!")]
Example1 expression
ipreview (le 1) $ IntMap.fromList [(1, 'x'), (2, 'y')]Just (1,'x')
valuege :: Int -> IxAffineTraversal' Int (IntMap v) v
#

Focus on the smallest key greater or equal than the given one and its corresponding value.

Example1 expression
IntMap.fromList [(1, "hi"), (3, "there")] & over (ge 2) (++ "!")fromList [(1,"hi"),(3,"there!")]
Example1 expression
ipreview (ge 2) $ IntMap.fromList [(1, 'x'), (3, 'y')]Just (3,'y')