HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleoptics-core-0.4.1.1Haskell2010

Data.Map.Optics

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
  • 5 values
valuetoMapOf
  1. :: (Is k A_Fold, HasSingleIndex is i, Ord i)
  2. => Optic' k is s a
  3. -> s
  4. -> Map i 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) [('a',"alpha"),('b', "beta")]fromList [('a',"alpha"),('b',"beta")]
Example1 expression
toMapOf (ifolded <%> ifolded) ["foo", "bar"]fromList [((0,0),'f'),((0,1),'o'),((0,2),'o'),((1,0),'b'),((1,1),'a'),((1,2),'r')]
Example1 expression
toMapOf (folded % ifolded) [('a', "hello"), ('b', "world"), ('a', "dummy")]fromList [('a',"hello"),('b',"world")]
valuelt :: Ord k => k -> IxAffineTraversal' k (Map k v) v
#

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

Example1 expression
Map.fromList [('a', "hi"), ('b', "there")] & over (lt 'b') (++ "!")fromList [('a',"hi!"),('b',"there")]
Example1 expression
ipreview (lt 'a') $ Map.fromList [('a', 'x'), ('b', 'y')]Nothing
valuegt :: Ord k => k -> IxAffineTraversal' k (Map k v) v
#

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

Example1 expression
Map.fromList [('a', "hi"), ('b', "there")] & over (gt 'b') (++ "!")fromList [('a',"hi"),('b',"there")]
Example1 expression
ipreview (gt 'a') $ Map.fromList [('a', 'x'), ('b', 'y')]Just ('b','y')
valuele :: Ord k => k -> IxAffineTraversal' k (Map k v) v
#

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

Example1 expression
Map.fromList [('a', "hi"), ('b', "there")] & over (le 'b') (++ "!")fromList [('a',"hi"),('b',"there!")]
Example1 expression
ipreview (le 'a') $ Map.fromList [('a', 'x'), ('b', 'y')]Just ('a','x')
valuege :: Ord k => k -> IxAffineTraversal' k (Map k v) v
#

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

Example1 expression
Map.fromList [('a', "hi"), ('c', "there")] & over (ge 'b') (++ "!")fromList [('a',"hi"),('c',"there!")]
Example1 expression
ipreview (ge 'b') $ Map.fromList [('a', 'x'), ('c', 'y')]Just ('c','y')