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]
This module includes the core definitions from Optics.At.Core along with extra (orphan) instances.
Synopsis
- type family Index s :: Type
- type family IxValue m :: Type
- 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 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 ByteString Source # | |
Defined in Optics.At | |
type Index ByteString Source # | |
Defined in Optics.At | |
type Index IntSet | |
Defined in Optics.At.Core | |
type Index Text Source # | |
type Index Text Source # | |
type Index [a] | |
Defined in Optics.At.Core | |
type Index (Maybe a) | |
Defined in Optics.At.Core | |
type Index (Complex a) | |
Defined in Optics.At.Core | |
type Index (Identity a) | |
Defined in Optics.At.Core | |
type Index (NonEmpty a) | |
Defined in Optics.At.Core | |
type Index (IntMap a) | |
Defined in Optics.At.Core | |
type Index (Tree a) | |
Defined in Optics.At.Core | |
type Index (Seq a) | |
Defined in Optics.At.Core | |
type Index (Set a) | |
Defined in Optics.At.Core | |
type Index (HashSet a) Source # | |
type Index (Vector a) Source # | |
type Index (Vector a) Source # | |
type Index (Vector a) Source # | |
type Index (Vector a) Source # | |
type Index (e -> a) | |
Defined in Optics.At.Core type Index (e -> a) = e | |
type Index (a, b) | |
Defined in Optics.At.Core | |
type Index (UArray i e) | |
Defined in Optics.At.Core | |
type Index (Array i e) | |
Defined in Optics.At.Core | |
type Index (Map k a) | |
Defined in Optics.At.Core | |
type Index (HashMap k a) Source # | |
type Index (a, b, c) | |
Defined in Optics.At.Core | |
type Index (a, b, c, d) | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e) | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f) | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f, g) | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f, g, h) | |
Defined in Optics.At.Core | |
type Index (a, b, c, d, e, f, g, h, i) | |
Defined in Optics.At.Core |
type family IxValue m :: Type #
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 ByteString Source # | |
Defined in Optics.At | |
type IxValue ByteString Source # | |
Defined in Optics.At | |
type IxValue IntSet | |
Defined in Optics.At.Core | |
type IxValue Text Source # | |
type IxValue Text Source # | |
type IxValue [a] | |
Defined in Optics.At.Core type IxValue [a] = a | |
type IxValue (Maybe a) | |
Defined in Optics.At.Core | |
type IxValue (Identity a) | |
Defined in Optics.At.Core | |
type IxValue (NonEmpty a) | |
Defined in Optics.At.Core | |
type IxValue (IntMap a) | |
Defined in Optics.At.Core | |
type IxValue (Tree a) | |
Defined in Optics.At.Core | |
type IxValue (Seq a) | |
Defined in Optics.At.Core | |
type IxValue (Set k) | |
Defined in Optics.At.Core | |
type IxValue (HashSet k) Source # | |
type IxValue (Vector a) Source # | |
type IxValue (Vector a) Source # | |
type IxValue (Vector a) Source # | |
type IxValue (Vector a) Source # | |
type IxValue (e -> a) | |
Defined in Optics.At.Core type IxValue (e -> a) = a | |
type IxValue (a0, a2) |
|
Defined in Optics.At.Core type IxValue (a0, a2) = a0 | |
type IxValue (UArray i e) | |
Defined in Optics.At.Core | |
type IxValue (Array i e) | |
Defined in Optics.At.Core | |
type IxValue (Map k a) | |
Defined in Optics.At.Core | |
type IxValue (HashMap k a) Source # | |
type IxValue (a0, a1, a2) |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2) = a0 | |
type IxValue (a0, a1, a2, a3) |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3) = a0 | |
type IxValue (a0, a1, a2, a3, a4) |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4) = a0 | |
type IxValue (a0, a1, a2, a3, a4, a5) |
|
Defined in Optics.At.Core type IxValue (a0, a1, a2, a3, a4, a5) = a0 | |
type IxValue (a0, a1, a2, a3, a4, a5, a6) |
|
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) |
|
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) |
|
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) #
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 ByteString Source # | |
Defined in Optics.At | |
Ixed ByteString Source # | |
Defined in Optics.At | |
Ixed IntSet | |
Defined in Optics.At.Core | |
Ixed Text Source # | |
Ixed Text Source # | |
Ixed [a] | |
Defined in Optics.At.Core ix :: Index [a] -> AffineTraversal' [a] (IxValue [a]) # | |
Ixed (Maybe a) | |
Defined in Optics.At.Core | |
Ixed (Identity a) | |
Defined in Optics.At.Core | |
Ixed (NonEmpty a) | |
Defined in Optics.At.Core | |
Ixed (IntMap a) | |
Defined in Optics.At.Core | |
Ixed (Tree a) | |
Defined in Optics.At.Core | |
Ixed (Seq a) | |
Defined in Optics.At.Core | |
Ord k => Ixed (Set k) | |
Defined in Optics.At.Core | |
(Eq k, Hashable k) => Ixed (HashSet k) Source # | |
Unbox a => Ixed (Vector a) Source # | |
Storable a => Ixed (Vector a) Source # | |
Prim a => Ixed (Vector a) Source # | |
Ixed (Vector a) Source # | |
Eq e => Ixed (e -> a) | |
Defined in Optics.At.Core ix :: Index (e -> a) -> AffineTraversal' (e -> a) (IxValue (e -> a)) # | |
a0 ~ a1 => Ixed (a0, a1) | |
Defined in Optics.At.Core ix :: Index (a0, a1) -> AffineTraversal' (a0, a1) (IxValue (a0, a1)) # | |
(IArray UArray e, Ix i) => Ixed (UArray i e) | arr |
Defined in Optics.At.Core | |
Ix i => Ixed (Array i e) | arr |
Defined in Optics.At.Core | |
Ord k => Ixed (Map k a) | |
Defined in Optics.At.Core | |
(Eq k, Hashable k) => Ixed (HashMap k a) Source # | |
(a0 ~ a1, a0 ~ a2) => Ixed (a0, a1, a2) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2) -> AffineTraversal' (a0, a1, a2) (IxValue (a0, a1, a2)) # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3) => Ixed (a0, a1, a2, a3) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2, a3) -> AffineTraversal' (a0, a1, a2, a3) (IxValue (a0, a1, a2, a3)) # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4) => Ixed (a0, a1, a2, a3, a4) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2, a3, a4) -> AffineTraversal' (a0, a1, a2, a3, a4) (IxValue (a0, a1, a2, a3, a4)) # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5) => Ixed (a0, a1, a2, a3, a4, a5) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2, a3, a4, a5) -> AffineTraversal' (a0, a1, a2, a3, a4, a5) (IxValue (a0, a1, a2, a3, a4, a5)) # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6) => Ixed (a0, a1, a2, a3, a4, a5, a6) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2, a3, a4, a5, a6) -> AffineTraversal' (a0, a1, a2, a3, a4, a5, a6) (IxValue (a0, a1, a2, a3, a4, a5, a6)) # | |
(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2, a3, a4, a5, a6, a7) -> AffineTraversal' (a0, a1, a2, a3, a4, a5, a6, a7) (IxValue (a0, a1, a2, a3, a4, a5, a6, a7)) # | |
(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) | |
Defined in Optics.At.Core ix :: Index (a0, a1, a2, a3, a4, a5, a6, a7, a8) -> AffineTraversal' (a0, a1, a2, a3, a4, a5, a6, a7, a8) (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)) # |
At
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)) #
>>>
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)) #
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
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.
contains :: Index m -> Lens' m Bool #
>>>
IntSet.fromList [1,2,3,4] ^. contains 3
True
>>>
IntSet.fromList [1,2,3,4] ^. contains 5
False
>>>
IntSet.fromList [1,2,3,4] & contains 3 .~ False
fromList [1,2,4]
Orphan instances
Ixed ByteString Source # | |
Ixed ByteString Source # | |
Ixed Text Source # | |
Ixed Text Source # | |
(Eq a, Hashable a) => Contains (HashSet a) Source # | |
(Eq k, Hashable k) => Ixed (HashSet k) Source # | |
Unbox a => Ixed (Vector a) Source # | |
Storable a => Ixed (Vector a) Source # | |
Prim a => Ixed (Vector a) Source # | |
Ixed (Vector a) Source # | |
(Eq k, Hashable k) => At (HashSet k) Source # | |
(Eq k, Hashable k) => Ixed (HashMap k a) Source # | |
(Eq k, Hashable k) => At (HashMap k a) Source # | |