microlens-0.1.3.0: A tiny part of the lens library which you can depend upon

Safe HaskellSafe
LanguageHaskell2010

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 traverse 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 traverse instead. Personally, I like using each instead of traverse whenever possible – it's shorter and more descriptive.

You can use each with these things:

each :: Traversal [a] [b] a b

each :: Traversal (Maybe a) (Maybe b) a b

each :: Traversal (a,a) (b,b) a b
each :: Traversal (a,a,a) (b,b,b) a b
each :: Traversal (a,a,a,a) (b,b,b,b) a b
each :: Traversal (a,a,a,a,a) (b,b,b,b,b) a b

each :: (RealFloat a, RealFloat b) => Traversal (Complex a) (Complex b) a b

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 

class Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source

Minimal complete definition

Nothing

Methods

_1 :: Lens s t a b Source

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).

_2, _3, _4, and _5 are also available (see below).

Instances

Field1 (a, b) (a', b) a a' Source 
Field1 (a, b, c) (a', b, c) a a' Source 
Field1 (a, b, c, d) (a', b, c, d) a a' Source 
Field1 (a, b, c, d, e) (a', b, c, d, e) a a' Source 

class Field2 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source

Minimal complete definition

Nothing

Methods

_2 :: Lens s t a b Source

Instances

Field2 (a, b) (a, b') b b' Source 
Field2 (a, b, c) (a, b', c) b b' Source 
Field2 (a, b, c, d) (a, b', c, d) b b' Source 
Field2 (a, b, c, d, e) (a, b', c, d, e) b b' Source 

class Field3 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source

Minimal complete definition

Nothing

Methods

_3 :: Lens s t a b Source

Instances

Field3 (a, b, c) (a, b, c') c c' Source 
Field3 (a, b, c, d) (a, b, c', d) c c' Source 
Field3 (a, b, c, d, e) (a, b, c', d, e) c c' Source 

class Field4 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source

Minimal complete definition

Nothing

Methods

_4 :: Lens s t a b Source

Instances

Field4 (a, b, c, d) (a, b, c, d') d d' Source 
Field4 (a, b, c, d, e) (a, b, c, d', e) d d' Source 

class Field5 s t a b | s -> a, t -> b, s b -> t, t a -> s where Source

Minimal complete definition

Nothing

Methods

_5 :: Lens s t a b Source

Instances

Field5 (a, b, c, d, e) (a, b, c, d, e') e e' Source