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 family IxValue (m :: *) :: *
- class Ixed m where
- ix :: Index m -> AffineTraversal' m (IxValue m)
- ixAt :: At m => Index m -> AffineTraversal' m (IxValue m)
- class Ixed m => 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 :: *) :: * 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 :: *) :: * 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
ix :: Index m -> AffineTraversal' 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
ix :: At m => Index m -> AffineTraversal' 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 # | |
Defined in Optics.At.Core | |
Ixed [a] Source # | |
Defined in Optics.At.Core | |
Ixed (Maybe a) Source # | |
Defined in Optics.At.Core | |
Ixed (Identity a) Source # | |
Defined in Optics.At.Core | |
Ixed (NonEmpty a) Source # | |
Defined in Optics.At.Core | |
Ixed (IntMap a) Source # | |
Defined in Optics.At.Core | |
Ixed (Tree a) Source # | |
Defined in Optics.At.Core | |
Ixed (Seq a) Source # | |
Defined in Optics.At.Core | |
Ord k => Ixed (Set k) Source # | |
Defined in Optics.At.Core | |
Eq e => Ixed (e -> a) Source # | |
Defined in Optics.At.Core | |
a0 ~ a1 => Ixed (a0, a1) Source # | |
Defined in Optics.At.Core | |
(IArray UArray e, Ix i) => Ixed (UArray i e) Source # | arr |
Defined in Optics.At.Core | |
Ix i => Ixed (Array i e) Source # | arr |
Defined in Optics.At.Core | |
Ord k => Ixed (Map k a) Source # | |
Defined in Optics.At.Core | |
(a0 ~ a1, a0 ~ a2) => Ixed (a0, a1, a2) Source # | |
Defined in Optics.At.Core | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3) => Ixed (a0, a1, a2, a3) Source # | |
Defined in Optics.At.Core | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4) => Ixed (a0, a1, a2, a3, a4) Source # | |
Defined in Optics.At.Core | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5) => Ixed (a0, a1, a2, a3, a4, a5) Source # | |
Defined in Optics.At.Core | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6) => Ixed (a0, a1, a2, a3, a4, a5, a6) Source # | |
Defined in Optics.At.Core | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7) Source # | |
Defined in Optics.At.Core | |
(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 # | |
Defined in Optics.At.Core |
At
class Ixed m => 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.