Safe Haskell | None |
---|---|
Language | Haskell2010 |
Lens.Micro.Classes
Documentation
class Each s t a b | s -> a, t -> b, s b -> t, t a -> s where Source
Minimal complete definition
Nothing
Methods
each :: Traversal s t a b Source
each
tries to be a universal Traversal
– it behaves like traversed
in most situations, but also adds support for e.g. tuples with same-typed values:
>>>
(1,2) & each %~ succ
(2,3)
>>>
["x", "y", "z"] ^. each
"xyz"
However, note that each
doesn't work on every instance of Traversable
. If you have a Traversable
which isn't supported by each
, you can use traversed
instead. Personally, I like using each
instead of traversed
whenever possible – it's shorter and more descriptive.
You can use each
with these things:
each
::Traversal
[a] [b] a beach
::Traversal
(Maybe
a) (Maybe
b) a beach
::Traversal
(a,a) (b,b) a beach
::Traversal
(a,a,a) (b,b,b) a beach
::Traversal
(a,a,a,a) (b,b,b,b) a beach
::Traversal
(a,a,a,a,a) (b,b,b,b,b) a beach
:: (RealFloat
a,RealFloat
b) =>Traversal
(Complex
a) (Complex
b) a b
Additionally, you can use each
with types from array, bytestring, and containers by importing Lens.Micro.GHC
from the microlens-ghc package.
Instances
Each [a] [b] a b Source | |
Each (Complex a) (Complex b) a b Source | |
Each (Maybe a) (Maybe b) a b Source | |
((~) * a b, (~) * q r) => Each (a, b) (q, r) a q Source | |
((~) * a b, (~) * a c, (~) * q r, (~) * q s) => Each (a, b, c) (q, r, s) a q Source | |
((~) * a b, (~) * a c, (~) * a d, (~) * q r, (~) * q s, (~) * q t) => Each (a, b, c, d) (q, r, s, t) a q Source | |
((~) * a b, (~) * a c, (~) * a d, (~) * a e, (~) * q r, (~) * q s, (~) * q t, (~) * q u) => Each (a, b, c, d, e) (q, r, s, t, u) a q Source |
Minimal complete definition
Nothing
Methods
ix :: Index m -> Traversal' m (IxValue m) Source
This traversal lets you access (and update) an arbitrary element in a list, array, Map
, etc. (If you want to insert or delete elements as well, look at at
.)
An example for lists:
>>>
[0..5] & ix 3 .~ 10
[0,1,2,100,4,5]
You can use it for getting, too:
>>>
[0..5] ^? ix 3
Just 3
Of course, the element may not be present (which means that you can use ix
as a safe variant of (!!
)):
>>>
[0..5] ^? ix 10
Nothing
Another useful instance is the one for functions – it lets you modify their outputs for specific inputs. For instance, here's maximum
that returns 0 when the list is empty (instead of throwing an exception):
maximum0 =maximum
&
ix
[].~
0
The following instances are provided in this package:
ix
::Int
->Traversal'
[a] aix
:: (Eq
e) => e ->Traversal'
(e -> a) a
Additionally, you can use ix
with types from array, bytestring, and containers by importing Lens.Micro.GHC
from the microlens-ghc package.
class Ixed m => At m where Source
Methods
at :: Index m -> Lens' m (Maybe (IxValue m)) Source
This lens lets you read, write, or delete elements in Map
-like structures. It returns Nothing
when the value isn't found, just like lookup
:
Data.Map.lookup k m = m ^.
at k
However, it also lets you insert and delete values by setting the value to
or Just
valueNothing
:
Data.Map.insert k a m = m&
at k.~
Just a Data.Map.delete k m = m&
at k.~
Nothing
at
doesn't work for arrays, because you can't delete an arbitrary element from an array.
If you want to modify an already existing value, you should use ix
instead because then you won't have to deal with Maybe
(ix
is available for all types that have at
).
This package doesn't actually provide any instances for at
, but you can import Lens.Micro.GHC
from the microlens-ghc package and get instances for Map
and IntMap
.
class Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source
Minimal complete definition
Nothing
Methods
Gives access to the 1st field of a tuple (up to 5-tuples).
Getting the 1st component:
>>>
(1,2,3,4,5) ^. _1
1
Setting the 1st component:
>>>
(1,2,3) & _1 .~ 10
(10,2,3)
Note that this lens is lazy, and can set fields even of undefined
:
>>>
set _1 10 undefined :: (Int, Int)
(10,*** Exception: Prelude.undefined
This is done to avoid violating a lens law stating that you can get back what you put:
>>>
view _1 . set _1 10 $ (undefined :: (Int, Int))
10
The implementation (for 2-tuples) is:
_1
f t = (,)<$>
f (fst
t)<*>
pure
(snd
t)
or, alternatively,
_1
f ~(a,b) = (\a' -> (a',b))<$>
f a
(where ~
means a lazy pattern).
class Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source
Minimal complete definition
Nothing
class Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source
Minimal complete definition
Nothing