lens-5.3.3: Lenses, Folds and Traversals

Copyright | (C) 2012-16 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | experimental
Portability | non-portable
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Lens.At

Contents

- At
- Ixed
- Contains

Description

Synopsis

- class Ixed m => At m where
  - at :: Index m -> Lens' m (Maybe (IxValue m))
- sans :: At m => Index m -> m -> m
- iat :: At m => Index m -> IndexedLens' (Index m) m (Maybe (IxValue m))
- type family Index s
- type family IxValue m
- class Ixed m where
  - ix :: Index m -> Traversal' m (IxValue m)
- ixAt :: At m => Index m -> Traversal' m (IxValue m)
- iix :: Ixed m => Index m -> IndexedTraversal' (Index m) m (IxValue m)
- class Contains m where
  - contains :: Index m -> Lens' m Bool
- icontains :: Contains m => Index m -> IndexedLens' (Index m) m Bool

# At

class Ixed m => At m where #

`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 . traverse

Methods

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: `Map`-like containers form a reasonable instance, but not `Array`-like ones, where you cannot satisfy the `Lens` laws.

#### Instances

Instances details

At IntSet # |
Instance details Defined in Control.Lens.At Methods at :: Index IntSet -> Lens' IntSet (Maybe (IxValue IntSet)) #
At (IntMap a) # |
Instance details Defined in Control.Lens.At Methods at :: Index (IntMap a) -> Lens' (IntMap a) (Maybe (IxValue (IntMap a))) #
Ord k => At (Set k) # |
Instance details Defined in Control.Lens.At Methods at :: Index (Set k) -> Lens' (Set k) (Maybe (IxValue (Set k))) #
(Eq k, Hashable k) => At (HashSet k) # |
Instance details Defined in Control.Lens.At Methods at :: Index (HashSet k) -> Lens' (HashSet k) (Maybe (IxValue (HashSet k))) #
At (Maybe a) # |
Instance details Defined in Control.Lens.At Methods at :: Index (Maybe a) -> Lens' (Maybe a) (Maybe (IxValue (Maybe a))) #
Ord k => At (Map k a) # |
Instance details Defined in Control.Lens.At Methods at :: Index (Map k a) -> Lens' (Map k a) (Maybe (IxValue (Map k a))) #
(Eq k, Hashable k) => At (HashMap k a) # |
Instance details Defined in Control.Lens.At Methods at :: Index (HashMap k a) -> Lens' (HashMap k a) (Maybe (IxValue (HashMap k a))) #

sans :: At m => Index m -> m -> m #

Delete the value associated with a key in a `Map`-like container

    sans k = at k .~ Nothing

iat :: At m => Index m -> IndexedLens' (Index m) m (Maybe (IxValue m)) #

An indexed version of `at`.

    >>> Map.fromList [(1,"world")] ^@. iat 1
    (1,Just "world")

    >>> iat 1 %@~ (\i x -> if odd i then Just "hello" else Nothing) $ Map.empty
    fromList [(1,"hello")]

    >>> iat 2 %@~ (\i x -> if odd i then Just "hello" else Nothing) $ Map.empty
    fromList []

# Ixed

type family Index s #

#### Instances

Instances details

type Index ByteString # |
Instance details Defined in Control.Lens.At type Index ByteString = Int
type Index ByteString # |
Instance details Defined in Control.Lens.At type Index ByteString = Int64
type Index IntSet # |
Instance details Defined in Control.Lens.At type Index IntSet = Int
type Index Text # |
Instance details Defined in Control.Lens.At type Index Text = Int
type Index Text # |
Instance details Defined in Control.Lens.At type Index Text = Int64
type Index (Complex a) # |
Instance details Defined in Control.Lens.At type Index (Complex a) = Int
type Index (IntMap a) # |
Instance details Defined in Control.Lens.At type Index (IntMap a) = Int
type Index (Seq a) # |
Instance details Defined in Control.Lens.At type Index (Seq a) = Int
type Index (Set a) # |
Instance details Defined in Control.Lens.At type Index (Set a) = a
type Index (Tree a) # |
Instance details Defined in Control.Lens.At type Index (Tree a) = [Int]
type Index (NonEmpty a) # |
Instance details Defined in Control.Lens.At type Index (NonEmpty a) = Int
type Index (Identity a) # |
Instance details Defined in Control.Lens.At type Index (Identity a) = ()
type Index (HashSet a) # |
Instance details Defined in Control.Lens.At type Index (HashSet a) = a
type Index (Vector a) # |
Instance details Defined in Control.Lens.At type Index (Vector a) = Int
type Index (Vector a) # |
Instance details Defined in Control.Lens.At type Index (Vector a) = Int
type Index (Vector a) # |
Instance details Defined in Control.Lens.At type Index (Vector a) = Int
type Index (Vector a) # |
Instance details Defined in Control.Lens.At type Index (Vector a) = Int
type Index (Maybe a) # |
Instance details Defined in Control.Lens.At type Index (Maybe a) = ()
type Index [a] # |
Instance details Defined in Control.Lens.At type Index [a] = Int
type Index (UArray i e) # |
Instance details Defined in Control.Lens.At type Index (UArray i e) = i
type Index (Map k a) # |
Instance details Defined in Control.Lens.At type Index (Map k a) = k
type Index (Array i e) # |
Instance details Defined in Control.Lens.At type Index (Array i e) = i
type Index (HashMap k a) # |
Instance details Defined in Control.Lens.At type Index (HashMap k a) = k
type Index (a, b) # |
Instance details Defined in Control.Lens.At type Index (a, b) = Int
type Index (e -> a) # |
Instance details Defined in Control.Lens.At type Index (e -> a) = e
type Index (a, b, c) # |
Instance details Defined in Control.Lens.At type Index (a, b, c) = Int
type Index (a, b, c, d) # |
Instance details Defined in Control.Lens.At type Index (a, b, c, d) = Int
type Index (a, b, c, d, e) # |
Instance details Defined in Control.Lens.At type Index (a, b, c, d, e) = Int
type Index (a, b, c, d, e, f) # |
Instance details Defined in Control.Lens.At type Index (a, b, c, d, e, f) = Int
type Index (a, b, c, d, e, f, g) # |
Instance details Defined in Control.Lens.At type Index (a, b, c, d, e, f, g) = Int
type Index (a, b, c, d, e, f, g, h) # |
Instance details Defined in Control.Lens.At type Index (a, b, c, d, e, f, g, h) = Int
type Index (a, b, c, d, e, f, g, h, i) # |
Instance details Defined in Control.Lens.At type Index (a, b, c, d, e, f, g, h, i) = Int

type family IxValue m #

This provides a common notion of a value at an index that is shared by both `Ixed` and `At`.

#### Instances

Instances details

type IxValue ByteString # |
Instance details Defined in Control.Lens.At type IxValue ByteString = Word8
type IxValue ByteString # |
Instance details Defined in Control.Lens.At type IxValue ByteString = Word8
type IxValue IntSet # |
Instance details Defined in Control.Lens.At type IxValue IntSet = ()
type IxValue Text # |
Instance details Defined in Control.Lens.At type IxValue Text = Char
type IxValue Text # |
Instance details Defined in Control.Lens.At type IxValue Text = Char
type IxValue (IntMap a) # |
Instance details Defined in Control.Lens.At type IxValue (IntMap a) = a
type IxValue (Seq a) # |
Instance details Defined in Control.Lens.At type IxValue (Seq a) = a
type IxValue (Set k) # |
Instance details Defined in Control.Lens.At type IxValue (Set k) = ()
type IxValue (Tree a) # |
Instance details Defined in Control.Lens.At type IxValue (Tree a) = a
type IxValue (NonEmpty a) # |
Instance details Defined in Control.Lens.At type IxValue (NonEmpty a) = a
type IxValue (Identity a) # |
Instance details Defined in Control.Lens.At type IxValue (Identity a) = a
type IxValue (HashSet k) # |
Instance details Defined in Control.Lens.At type IxValue (HashSet k) = ()
type IxValue (Vector a) # |
Instance details Defined in Control.Lens.At type IxValue (Vector a) = a
type IxValue (Vector a) # |
Instance details Defined in Control.Lens.At type IxValue (Vector a) = a
type IxValue (Vector a) # |
Instance details Defined in Control.Lens.At type IxValue (Vector a) = a
type IxValue (Vector a) # |
Instance details Defined in Control.Lens.At type IxValue (Vector a) = a
type IxValue (Maybe a) # |
Instance details Defined in Control.Lens.At type IxValue (Maybe a) = a
type IxValue [a] # |
Instance details Defined in Control.Lens.At type IxValue [a] = a
type IxValue (UArray i e) # |
Instance details Defined in Control.Lens.At type IxValue (UArray i e) = e
type IxValue (Map k a) # |
Instance details Defined in Control.Lens.At type IxValue (Map k a) = a
type IxValue (Array i e) # |
Instance details Defined in Control.Lens.At type IxValue (Array i e) = e
type IxValue (HashMap k a) # |
Instance details Defined in Control.Lens.At type IxValue (HashMap k a) = a

type IxValue (a, a2) #

    ix :: Int -> Traversal' (a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2) = a
type IxValue (e -> a) # |
Instance details Defined in Control.Lens.At type IxValue (e -> a) = a

type IxValue (a, a2, a3) #

    ix :: Int -> Traversal' (a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3) = a

type IxValue (a, a2, a3, a4) #

    ix :: Int -> Traversal' (a,a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3, a4) = a

type IxValue (a, a2, a3, a4, a5) #

    ix :: Int -> Traversal' (a,a,a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3, a4, a5) = a

type IxValue (a, a2, a3, a4, a5, a6) #

    ix :: Int -> Traversal' (a,a,a,a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6) = a

type IxValue (a, a2, a3, a4, a5, a6, a7) #

    ix :: Int -> Traversal' (a,a,a,a,a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6, a7) = a

type IxValue (a, a2, a3, a4, a5, a6, a7, a8) #

    ix :: Int -> Traversal' (a,a,a,a,a,a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6, a7, a8) = a

type IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9) #

    ix :: Int -> Traversal' (a,a,a,a,a,a,a,a,a) a

Instance details Defined in Control.Lens.At type IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9) = a

class Ixed m where #

Provides a simple `Traversal` lets you `traverse` the value at a given key in a `Map` or element at an ordinal position in a list or `Seq`.

Minimal complete definition

Nothing

Methods

ix :: Index m -> Traversal' m (IxValue m) #

NB: Setting the value of this `Traversal` 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`.

    >>> Seq.fromList [a,b,c,d] & ix 2 %~ f
    fromList [a,b,f c,d]

    >>> Seq.fromList [a,b,c,d] & ix 2 .~ e
    fromList [a,b,e,d]

    >>> Seq.fromList [a,b,c,d] ^? ix 2
    Just c

    >>> Seq.fromList [] ^? ix 2
    Nothing

default ix :: At m => Index m -> Traversal' m (IxValue m) #

#### Instances

Instances details

Ixed ByteString # |
Instance details Defined in Control.Lens.At Methods ix :: Index ByteString -> Traversal' ByteString (IxValue ByteString) #
Ixed ByteString # |
Instance details Defined in Control.Lens.At Methods ix :: Index ByteString -> Traversal' ByteString (IxValue ByteString) #
Ixed IntSet # |
Instance details Defined in Control.Lens.At Methods ix :: Index IntSet -> Traversal' IntSet (IxValue IntSet) #
Ixed Text # |
Instance details Defined in Control.Lens.At Methods ix :: Index Text -> Traversal' Text (IxValue Text) #
Ixed Text # |
Instance details Defined in Control.Lens.At Methods ix :: Index Text -> Traversal' Text (IxValue Text) #
Ixed (IntMap a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (IntMap a) -> Traversal' (IntMap a) (IxValue (IntMap a)) #
Ixed (Seq a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Seq a) -> Traversal' (Seq a) (IxValue (Seq a)) #
Ord k => Ixed (Set k) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Set k) -> Traversal' (Set k) (IxValue (Set k)) #
Ixed (Tree a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Tree a) -> Traversal' (Tree a) (IxValue (Tree a)) #
Ixed (NonEmpty a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (NonEmpty a) -> Traversal' (NonEmpty a) (IxValue (NonEmpty a)) #
Ixed (Identity a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Identity a) -> Traversal' (Identity a) (IxValue (Identity a)) #
(Eq k, Hashable k) => Ixed (HashSet k) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (HashSet k) -> Traversal' (HashSet k) (IxValue (HashSet k)) #
Ixed (Vector a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) #
Prim a => Ixed (Vector a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) #
Storable a => Ixed (Vector a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) #
Unbox a => Ixed (Vector a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) #
Ixed (Maybe a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Maybe a) -> Traversal' (Maybe a) (IxValue (Maybe a)) #
Ixed [a] # |
Instance details Defined in Control.Lens.At Methods ix :: Index [a] -> Traversal' [a] (IxValue [a]) #

(IArray UArray e, Ix i) => Ixed (UArray i e) #

    arr ! i ≡ arr ^. ix i
    arr // [(i,e)] ≡ ix i .~ e $ arr

Instance details Defined in Control.Lens.At Methods ix :: Index (UArray i e) -> Traversal' (UArray i e) (IxValue (UArray i e)) #
Ord k => Ixed (Map k a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (Map k a) -> Traversal' (Map k a) (IxValue (Map k a)) #

Ix i => Ixed (Array i e) #

    arr ! i ≡ arr ^. ix i
    arr // [(i,e)] ≡ ix i .~ e $ arr

Instance details Defined in Control.Lens.At Methods ix :: Index (Array i e) -> Traversal' (Array i e) (IxValue (Array i e)) #
(Eq k, Hashable k) => Ixed (HashMap k a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (HashMap k a) -> Traversal' (HashMap k a) (IxValue (HashMap k a)) #
a ~ a2 => Ixed (a, a2) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2) -> Traversal' (a, a2) (IxValue (a, a2)) #
Eq e => Ixed (e -> a) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (e -> a) -> Traversal' (e -> a) (IxValue (e -> a)) #
(a ~ a2, a ~ a3) => Ixed (a, a2, a3) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3) -> Traversal' (a, a2, a3) (IxValue (a, a2, a3)) #
(a ~ a2, a ~ a3, a ~ a4) => Ixed (a, a2, a3, a4) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3, a4) -> Traversal' (a, a2, a3, a4) (IxValue (a, a2, a3, a4)) #
(a ~ a2, a ~ a3, a ~ a4, a ~ a5) => Ixed (a, a2, a3, a4, a5) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3, a4, a5) -> Traversal' (a, a2, a3, a4, a5) (IxValue (a, a2, a3, a4, a5)) #
(a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6) => Ixed (a, a2, a3, a4, a5, a6) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3, a4, a5, a6) -> Traversal' (a, a2, a3, a4, a5, a6) (IxValue (a, a2, a3, a4, a5, a6)) #
(a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6, a ~ a7) => Ixed (a, a2, a3, a4, a5, a6, a7) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3, a4, a5, a6, a7) -> Traversal' (a, a2, a3, a4, a5, a6, a7) (IxValue (a, a2, a3, a4, a5, a6, a7)) #
(a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6, a ~ a7, a ~ a8) => Ixed (a, a2, a3, a4, a5, a6, a7, a8) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3, a4, a5, a6, a7, a8) -> Traversal' (a, a2, a3, a4, a5, a6, a7, a8) (IxValue (a, a2, a3, a4, a5, a6, a7, a8)) #
(a ~ a2, a ~ a3, a ~ a4, a ~ a5, a ~ a6, a ~ a7, a ~ a8, a ~ a9) => Ixed (a, a2, a3, a4, a5, a6, a7, a8, a9) # |
Instance details Defined in Control.Lens.At Methods ix :: Index (a, a2, a3, a4, a5, a6, a7, a8, a9) -> Traversal' (a, a2, a3, a4, a5, a6, a7, a8, a9) (IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9)) #

ixAt :: At m => Index m -> Traversal' m (IxValue m) #

A definition of `ix` for types with an `At` instance. This is the default if you don't specify a definition for `ix`.

iix :: Ixed m => Index m -> IndexedTraversal' (Index m) m (IxValue m) #

An indexed version of `ix`.

    >>> Seq.fromList [a,b,c,d] & iix 2 %@~ f'
    fromList [a,b,f' 2 c,d]

    >>> Seq.fromList [a,b,c,d] & iix 2 .@~ h
    fromList [a,b,h 2,d]

    >>> Seq.fromList [a,b,c,d] ^@? iix 2
    Just (2,c)

    >>> Seq.fromList [] ^@? iix 2
    Nothing

# Contains

class Contains m where #

This class provides a simple `Lens` that lets you view (and modify) information about whether or not a container contains a given `Index`.

Methods

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]

#### Instances

Instances details

Contains IntSet # |
Instance details Defined in Control.Lens.At Methods contains :: Index IntSet -> Lens' IntSet Bool #
Ord a => Contains (Set a) # |
Instance details Defined in Control.Lens.At Methods contains :: Index (Set a) -> Lens' (Set a) Bool #
(Eq a, Hashable a) => Contains (HashSet a) # |
Instance details Defined in Control.Lens.At Methods contains :: Index (HashSet a) -> Lens' (HashSet a) Bool #

icontains :: Contains m => Index m -> IndexedLens' (Index m) m Bool #

An indexed version of `contains`.

    >>> IntSet.fromList [1,2,3,4] ^@. icontains 3
    (3,True)

    >>> IntSet.fromList [1,2,3,4] ^@. icontains 5
    (5,False)

    >>> IntSet.fromList [1,2,3,4] & icontains 3 %@~ \i x -> if odd i then not x else x
    fromList [1,2,4]

    >>> IntSet.fromList [1,2,3,4] & icontains 3 %@~ \i x -> if even i then not x else x
    fromList [1,2,3,4]