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

Language | Haskell2010 |

`IntMap`

is an instance of `At`

and provides
`at`

as a lens on values at keys:

`>>>`

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

`>>>`

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

`>>>`

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

We can traverse, fold over, and map over key-value pairs in an `IntMap`

,
thanks to indexed traversals, folds and setters.

`>>>`

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

`>>>`

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

`>>>`

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

`>>>`

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

A related class, `Ixed`

, allows us to use `ix`

to
traverse a value at a particular key.

`>>>`

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

`>>>`

Nothing`preview (ix 8) IntMap.empty`

# Documentation

toMapOf :: (Is k A_Fold, is `HasSingleIndex` Int) => Optic' k is s a -> s -> IntMap 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 [(1,"alpha"),(2,"beta")]`toMapOf (folded % ifolded) [(1,"alpha"),(2, "beta")]`

`>>>`

fromList [(0,'f'),(1,'o'),(2,'o'),(10,'b'),(11,'a'),(12,'r')]`toMapOf (icompose (\a b -> 10*a+b) $ ifolded % ifolded) ["foo", "bar"]`

`>>>`

fromList [(1,"hello"),(2,"world")]`toMapOf (folded % ifolded) [(1, "hello"), (2, "world"), (1, "dummy")]`

lt :: Int -> IxAffineTraversal' Int (IntMap v) v Source #

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

`>>>`

fromList [(1,"hi!"),(2,"there")]`IntMap.fromList [(1, "hi"), (2, "there")] & over (lt 2) (++ "!")`

`>>>`

Nothing`ipreview (lt 1) $ IntMap.fromList [(1, 'x'), (2, 'y')]`

gt :: Int -> IxAffineTraversal' Int (IntMap v) v Source #

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

`>>>`

fromList [(1,"hi"),(2,"there")]`IntMap.fromList [(1, "hi"), (2, "there")] & over (gt 2) (++ "!")`

`>>>`

Just (2,'y')`ipreview (gt 1) $ IntMap.fromList [(1, 'x'), (2, 'y')]`

le :: Int -> IxAffineTraversal' Int (IntMap v) v Source #

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

`>>>`

fromList [(1,"hi"),(2,"there!")]`IntMap.fromList [(1, "hi"), (2, "there")] & over (le 2) (++ "!")`

`>>>`

Just (1,'x')`ipreview (le 1) $ IntMap.fromList [(1, 'x'), (2, 'y')]`

ge :: Int -> IxAffineTraversal' Int (IntMap v) v Source #

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

`>>>`

fromList [(1,"hi"),(3,"there!")]`IntMap.fromList [(1, "hi"), (3, "there")] & over (ge 2) (++ "!")`

`>>>`

Just (3,'y')`ipreview (ge 2) $ IntMap.fromList [(1, 'x'), (3, 'y')]`