Safe Haskell | None |
---|---|

Language | Haskell2010 |

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:

`>>>`

Just "world"`Map.fromList [(1, "world")] ^. at 1`

`>>>`

fromList [(1,"world")]`Map.empty & at 1 .~ Just "world"`

`>>>`

fromList [(0,"hello")]`Map.empty & at 0 .~ Just "hello"`

We can traverse, fold over, and map over key-value pairs in a `Map`

,
thanks to indexed traversals, folds and setters.

`>>>`

fromList [(1,1)]`iover imapped const $ Map.fromList [(1, "Venus")]`

`>>>`

Sum {getSum = 5}`ifoldMapOf ifolded (\i _ -> Sum i) $ Map.fromList [(2, "Earth"), (3, "Mars")]`

`>>>`

(4,"Jupiter")`itraverseOf_ ifolded (curry print) $ Map.fromList [(4, "Jupiter")]`

`>>>`

[(5,"Saturn")]`itoListOf ifolded $ Map.fromList [(5, "Saturn")]`

A related class, `Ixed`

, allows us to use `ix`

to
traverse a value at a particular key.

`>>>`

fromList [(2,"New Earth")]`Map.fromList [(2, "Earth")] & ix 2 %~ ("New " ++)`

`>>>`

Nothing`preview (ix 8) Map.empty`

## Synopsis

- toMapOf :: (Is k A_Fold, is `HasSingleIndex` i, Ord i) => Optic' k is s a -> s -> Map i a
- lt :: Ord k => k -> IxAffineTraversal' k (Map k v) v
- gt :: Ord k => k -> IxAffineTraversal' k (Map k v) v
- le :: Ord k => k -> IxAffineTraversal' k (Map k v) v
- ge :: Ord k => k -> IxAffineTraversal' k (Map k v) v

# Documentation

toMapOf :: (Is k A_Fold, is `HasSingleIndex` i, Ord i) => Optic' k is s a -> s -> Map i a Source #

Construct a map from an `IxFold`

.

The construction is left-biased (see `union`

), i.e. the first
occurences of keys in the fold or traversal order are preferred.

`>>>`

fromList [(0,"hello"),(1,"world")]`toMapOf ifolded ["hello", "world"]`

`>>>`

fromList [('a',"alpha"),('b',"beta")]`toMapOf (folded % ifolded) [('a',"alpha"),('b', "beta")]`

`>>>`

fromList [((0,0),'f'),((0,1),'o'),((0,2),'o'),((1,0),'b'),((1,1),'a'),((1,2),'r')]`toMapOf (ifolded <%> ifolded) ["foo", "bar"]`

`>>>`

fromList [('a',"hello"),('b',"world")]`toMapOf (folded % ifolded) [('a', "hello"), ('b', "world"), ('a', "dummy")]`

lt :: Ord k => k -> IxAffineTraversal' k (Map k v) v Source #

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

`>>>`

fromList [('a',"hi!"),('b',"there")]`Map.fromList [('a', "hi"), ('b', "there")] & over (lt 'b') (++ "!")`

`>>>`

Nothing`ipreview (lt 'a') $ Map.fromList [('a', 'x'), ('b', 'y')]`

gt :: Ord k => k -> IxAffineTraversal' k (Map k v) v Source #

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

`>>>`

fromList [('a',"hi"),('b',"there")]`Map.fromList [('a', "hi"), ('b', "there")] & over (gt 'b') (++ "!")`

`>>>`

Just ('b','y')`ipreview (gt 'a') $ Map.fromList [('a', 'x'), ('b', 'y')]`

le :: Ord k => k -> IxAffineTraversal' k (Map k v) v Source #

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

`>>>`

fromList [('a',"hi"),('b',"there!")]`Map.fromList [('a', "hi"), ('b', "there")] & over (le 'b') (++ "!")`

`>>>`

Just ('a','x')`ipreview (le 'a') $ Map.fromList [('a', 'x'), ('b', 'y')]`

ge :: Ord k => k -> IxAffineTraversal' k (Map k v) v Source #

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

`>>>`

fromList [('a',"hi"),('c',"there!")]`Map.fromList [('a', "hi"), ('c', "there")] & over (ge 'b') (++ "!")`

`>>>`

Just ('c','y')`ipreview (ge 'b') $ Map.fromList [('a', 'x'), ('c', 'y')]`