Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides optics for Map
and Set
-like containers, including an
AffineTraversal
to traverse a key in a map or an element of a sequence:
>>>
preview (ix 1) ['a','b','c']
Just 'b'
a Lens
to get, set or delete a key in a map:
>>>
set (at 0) (Just 'b') (Map.fromList [(0, 'a')])
fromList [(0,'b')]
and a Lens
to insert or remove an element of a set:
>>>
IntSet.fromList [1,2,3,4] & contains 3 .~ False
fromList [1,2,4]
The Optics.At
module from optics-extra
provides additional instances of
the classes defined here.
Synopsis
- type family Index (s :: Type) :: Type
- type family IxValue (m :: Type) :: Type
- class Ixed m where
- ixAt :: At m => Index m -> AffineTraversal' m (IxValue m)
- class (Ixed m, IxKind m ~ An_AffineTraversal) => At m where
- at' :: At m => Index m -> Lens' m (Maybe (IxValue m))
- sans :: At m => Index m -> m -> m
- class Contains m where
Type families
type family Index (s :: Type) :: Type Source #
Type family that takes a key-value container type and returns the type of
keys (indices) into the container, for example
.
This is shared by Index
(Map
k a) ~ kIxed
, At
and Contains
.
Instances
type Index IntSet Source # | |
Defined in Optics.At.Core | |
type Index [a] Source # | |
Defined in Optics.At.Core | |
type Index (Maybe a) Source # | |
Defined in Optics.At.Core | |
type Index (Complex a) Source # | |
Defined in Optics.At.Core | |
type Index (Identity a) Source # | |
Defined in Optics.At.Core | |
type Index (NonEmpty a) Source # | |
Defined in Optics.At.Core | |
type Index (IntMap a) Source # | |
Defined in Optics.At.Core | |
type Index (Tree a) Source # | |
Defined in Optics.At.Core | |
type Index (Seq a) Source # | |
Defined in Optics.At.Core | |
type Index (Set a) Source # | |
Defined in Optics.At.Core | |
type Index (e -> a) Source # | |
Defined in Optics.At.Core type Index (e -> a) = e | |
type Index (a, b) Source # | |
Defined in Optics.At.Core | |
type Index (UArray i e) Source # | |
Defined in Optics.At.Core | |
type Index (Array i e) Source # | |
Defined in Optics.At.Core | |
type Index (Map k a) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c, d) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f, g) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f, g, h) Source # | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f, g, h, i) Source # | |
Defined in Optics.At.Core |
type family IxValue (m :: Type) :: Type Source #
Type family that takes a key-value container type and returns the type of
values stored in the container, for example
. This
is shared by both IxValue
(Map
k a) ~ aIxed
and At
.
Instances
type IxValue IntSet Source # | |
Defined in Optics.At.Core | |
type IxValue [a] Source # | |
Defined in Optics.At.Core type IxValue [a] = a | |
type IxValue (Maybe a) Source # | |
Defined in Optics.At.Core | |
type IxValue (Identity a) Source # | |
Defined in Optics.At.Core | |
type IxValue (NonEmpty a) Source # | |
Defined in Optics.At.Core | |
type IxValue (IntMap a) Source # | |
Defined in Optics.At.Core | |
type IxValue (Tree a) Source # | |
Defined in Optics.At.Core | |
type IxValue (Seq a) Source # | |
Defined in Optics.At.Core | |
type IxValue (Set k) Source # | |
Defined in Optics.At.Core | |
type IxValue (e -> a) Source # | |
Defined in Optics.At.Core type IxValue (e -> a) = a | |
type IxValue (a0, a2) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a2) = a0 | |
type IxValue (UArray i e) Source # | |
Defined in Optics.At.Core | |
type IxValue (Array i e) Source # | |
Defined in Optics.At.Core | |
type IxValue (Map k a) Source # | |
Defined in Optics.At.Core | |
type IxValue (a0, a1, a2) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2) = a0 | |
type IxValue (a0, a1, a2, a3) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3) = a0 | |
type IxValue (a0, a1, a2, a3, a4) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4) = a0 | |
type IxValue (a0, a1, a2, a3, a4, a5) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5) = a0 | |
type IxValue (a0, a1, a2, a3, a4, a5, a6) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5, a6) = a0 | |
type IxValue (a0, a1, a2, a3, a4, a5, a6, a7) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5, a6, a7) = a0 | |
type IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8) Source # |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8) = a0 |
Ixed
Provides a simple AffineTraversal
lets you traverse the value at a given
key in a Map
or element at an ordinal position in a list or Seq
.
Nothing
type IxKind (m :: Type) :: OpticKind Source #
Type family that takes a key-value container type and returns the kind
of optic to index into it. For most containers, it's An_AffineTraversal
,
Representable
(Naperian) containers it is A_Lens
, and multi-maps would
have A_Traversal
.
type IxKind m = An_AffineTraversal
ix :: Index m -> Optic' (IxKind m) NoIx m (IxValue m) Source #
NB: Setting the value of this AffineTraversal
will only set the value
in at
if it is already present.
If you want to be able to insert missing values, you want at
.
>>>
[1,2,3,4] & ix 2 %~ (*10)
[1,2,30,4]
>>>
"abcd" & ix 2 .~ 'e'
"abed"
>>>
"abcd" ^? ix 2
Just 'c'
>>>
[] ^? ix 2
Nothing
Instances
Ixed IntSet Source # | |
Ixed [a] Source # | |
Ixed (Maybe a) Source # | |
Ixed (Identity a) Source # | |
Ixed (NonEmpty a) Source # | |
Ixed (IntMap a) Source # | |
Ixed (Tree a) Source # | |
Ixed (Seq a) Source # | |
Ord k => Ixed (Set k) Source # | |
Eq e => Ixed (e -> a) Source # | |
a0 ~ a1 => Ixed (a0, a1) Source # | |
(IArray UArray e, Ix i) => Ixed (UArray i e) Source # | arr |
Ix i => Ixed (Array i e) Source # | arr |
Ord k => Ixed (Map k a) Source # | |
(a0 ~ a1, a0 ~ a2) => Ixed (a0, a1, a2) Source # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3) => Ixed (a0, a1, a2, a3) Source # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4) => Ixed (a0, a1, a2, a3, a4) Source # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5) => Ixed (a0, a1, a2, a3, a4, a5) Source # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6) => Ixed (a0, a1, a2, a3, a4, a5, a6) Source # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7) Source # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7, a0 ~ a8) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7, a8) Source # | |
At
class (Ixed m, IxKind m ~ An_AffineTraversal) => At m where Source #
At
provides a Lens
that can be used to read, write or delete the value
associated with a key in a Map
-like container on an ad hoc basis.
An instance of At
should satisfy:
ix
k ≡at
k%
_Just
at :: Index m -> Lens' m (Maybe (IxValue m)) Source #
>>>
Map.fromList [(1,"world")] ^. at 1
Just "world"
>>>
at 1 ?~ "hello" $ Map.empty
fromList [(1,"hello")]
Note: Usage of this function might introduce space leaks if you're not
careful to make sure that values put inside the Just
constructor are
evaluated. To force the values and avoid such leaks, use at'
instead.
Note: Map
-like containers form a reasonable instance, but not
Array
-like ones, where you cannot satisfy the Lens
laws.
at' :: At m => Index m -> Lens' m (Maybe (IxValue m)) Source #
Version of at
strict in the value inside the Just
constructor.
Example:
>>>
(at () .~ Just (error "oops") $ Nothing) `seq` ()
()
>>>
(at' () .~ Just (error "oops") $ Nothing) `seq` ()
*** Exception: oops ...
>>>
view (at ()) (Just $ error "oops") `seq` ()
()
>>>
view (at' ()) (Just $ error "oops") `seq` ()
*** Exception: oops ...
It also works as expected for other data structures:
>>>
(at 1 .~ Just (error "oops") $ Map.empty) `seq` ()
()
>>>
(at' 1 .~ Just (error "oops") $ Map.empty) `seq` ()
*** Exception: oops ...
Contains
class Contains m where Source #
This class provides a simple Lens
that lets you view (and modify)
information about whether or not a container contains a given Index
.
Instances are provided for Set
-like containers only.