optics-core-0.4.1: Optics as an abstract interface: core definitions
Safe HaskellNone
LanguageHaskell2010

Optics.At.Core

Description

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 families

type family Index (s :: Type) :: Type Source #

Type family that takes a key-value container type and returns the type of keys (indices) into the container, for example Index (Map k a) ~ k. This is shared by Ixed, At and Contains.

Instances

Instances details
type Index IntSet Source # 
Instance details

Defined in Optics.At.Core

type Index [a] Source # 
Instance details

Defined in Optics.At.Core

type Index [a] = Int
type Index (Maybe a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Maybe a) = ()
type Index (Complex a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Complex a) = Int
type Index (Identity a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Identity a) = ()
type Index (NonEmpty a) Source # 
Instance details

Defined in Optics.At.Core

type Index (NonEmpty a) = Int
type Index (IntMap a) Source # 
Instance details

Defined in Optics.At.Core

type Index (IntMap a) = Int
type Index (Tree a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Tree a) = [Int]
type Index (Seq a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Seq a) = Int
type Index (Set a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Set a) = a
type Index (e -> a) Source # 
Instance details

Defined in Optics.At.Core

type Index (e -> a) = e
type Index (a, b) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b) = Int
type Index (UArray i e) Source # 
Instance details

Defined in Optics.At.Core

type Index (UArray i e) = i
type Index (Array i e) Source # 
Instance details

Defined in Optics.At.Core

type Index (Array i e) = i
type Index (Map k a) Source # 
Instance details

Defined in Optics.At.Core

type Index (Map k a) = k
type Index (a, b, c) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c) = Int
type Index (a, b, c, d) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c, d) = Int
type Index (a, b, c, d, e) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c, d, e) = Int
type Index (a, b, c, d, e, f) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c, d, e, f) = Int
type Index (a, b, c, d, e, f, g) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c, d, e, f, g) = Int
type Index (a, b, c, d, e, f, g, h) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c, d, e, f, g, h) = Int
type Index (a, b, c, d, e, f, g, h, i) Source # 
Instance details

Defined in Optics.At.Core

type Index (a, b, c, d, e, f, g, h, i) = Int

type family IxValue (m :: Type) :: Type Source #

Type family that takes a key-value container type and returns the type of values stored in the container, for example IxValue (Map k a) ~ a. This is shared by both Ixed and At.

Instances

Instances details
type IxValue IntSet Source # 
Instance details

Defined in Optics.At.Core

type IxValue IntSet = ()
type IxValue [a] Source # 
Instance details

Defined in Optics.At.Core

type IxValue [a] = a
type IxValue (Maybe a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Maybe a) = a
type IxValue (Identity a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Identity a) = a
type IxValue (NonEmpty a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (NonEmpty a) = a
type IxValue (IntMap a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (IntMap a) = a
type IxValue (Tree a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Tree a) = a
type IxValue (Seq a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Seq a) = a
type IxValue (Set k) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Set k) = ()
type IxValue (e -> a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (e -> a) = a
type IxValue (a0, a2) Source #
ix :: Int -> AffineTraversal' (a, a) a
Instance details

Defined in Optics.At.Core

type IxValue (a0, a2) = a0
type IxValue (UArray i e) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (UArray i e) = e
type IxValue (Array i e) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Array i e) = e
type IxValue (Map k a) Source # 
Instance details

Defined in Optics.At.Core

type IxValue (Map k a) = a
type IxValue (a0, a1, a2) Source #
ix :: Int -> AffineTraversal' (a, a, a) a
Instance details

Defined in Optics.At.Core

type IxValue (a0, a1, a2) = a0
type IxValue (a0, a1, a2, a3) Source #
ix :: Int -> AffineTraversal' (a, a, a, a) a
Instance details

Defined in Optics.At.Core

type IxValue (a0, a1, a2, a3) = a0
type IxValue (a0, a1, a2, a3, a4) Source #
ix :: Int -> AffineTraversal' (a, a, a, a, a) a
Instance details

Defined in Optics.At.Core

type IxValue (a0, a1, a2, a3, a4) = a0
type IxValue (a0, a1, a2, a3, a4, a5) Source #
ix :: Int -> AffineTraversal' (a, a, a, a, a, a) a
Instance details

Defined in Optics.At.Core

type IxValue (a0, a1, a2, a3, a4, a5) = a0
type IxValue (a0, a1, a2, a3, a4, a5, a6) Source #
ix :: Int -> AffineTraversal' (a, a, a, a, a, a, a) a
Instance details

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 #
ix :: Int -> AffineTraversal' (a, a, a, a, a, a, a, a) a
Instance details

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 #
ix :: Int -> AffineTraversal' (a, a, a, a, a, a, a, a, a) a
Instance details

Defined in Optics.At.Core

type IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8) = a0

Ixed

class Ixed m where Source #

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.

Minimal complete definition

Nothing

Associated Types

type IxKind (m :: Type) :: OpticKind Source #

Type family that takes a key-value container type and returns the kind of optic to index into it. For most containers, it's An_AffineTraversal, Representable (Naperian) containers it is A_Lens, and multi-maps would have A_Traversal.

Methods

ix :: Index m -> Optic' (IxKind m) NoIx 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

default ix :: (At m, IxKind m ~ An_AffineTraversal) => Index m -> Optic' (IxKind m) NoIx m (IxValue m) Source #

Instances

Instances details
Ixed IntSet Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind IntSet Source #

Ixed [a] Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind [a] Source #

Methods

ix :: Index [a] -> Optic' (IxKind [a]) NoIx [a] (IxValue [a]) Source #

Ixed (Maybe a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Maybe a) Source #

Methods

ix :: Index (Maybe a) -> Optic' (IxKind (Maybe a)) NoIx (Maybe a) (IxValue (Maybe a)) Source #

Ixed (Identity a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Identity a) Source #

Ixed (NonEmpty a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (NonEmpty a) Source #

Ixed (IntMap a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (IntMap a) Source #

Methods

ix :: Index (IntMap a) -> Optic' (IxKind (IntMap a)) NoIx (IntMap a) (IxValue (IntMap a)) Source #

Ixed (Tree a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Tree a) Source #

Methods

ix :: Index (Tree a) -> Optic' (IxKind (Tree a)) NoIx (Tree a) (IxValue (Tree a)) Source #

Ixed (Seq a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Seq a) Source #

Methods

ix :: Index (Seq a) -> Optic' (IxKind (Seq a)) NoIx (Seq a) (IxValue (Seq a)) Source #

Ord k => Ixed (Set k) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Set k) Source #

Methods

ix :: Index (Set k) -> Optic' (IxKind (Set k)) NoIx (Set k) (IxValue (Set k)) Source #

Eq e => Ixed (e -> a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (e -> a) Source #

Methods

ix :: Index (e -> a) -> Optic' (IxKind (e -> a)) NoIx (e -> a) (IxValue (e -> a)) Source #

a0 ~ a1 => Ixed (a0, a1) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1) Source #

Methods

ix :: Index (a0, a1) -> Optic' (IxKind (a0, a1)) NoIx (a0, a1) (IxValue (a0, a1)) Source #

(IArray UArray e, Ix i) => Ixed (UArray i e) Source #
arr ! i ≡ arr ^. ix i
arr // [(i,e)] ≡ ix i .~ e $ arr
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (UArray i e) Source #

Methods

ix :: Index (UArray i e) -> Optic' (IxKind (UArray i e)) NoIx (UArray i e) (IxValue (UArray i e)) Source #

Ix i => Ixed (Array i e) Source #
arr ! i ≡ arr ^. ix i
arr // [(i,e)] ≡ ix i .~ e $ arr
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Array i e) Source #

Methods

ix :: Index (Array i e) -> Optic' (IxKind (Array i e)) NoIx (Array i e) (IxValue (Array i e)) Source #

Ord k => Ixed (Map k a) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (Map k a) Source #

Methods

ix :: Index (Map k a) -> Optic' (IxKind (Map k a)) NoIx (Map k a) (IxValue (Map k a)) Source #

(a0 ~ a1, a0 ~ a2) => Ixed (a0, a1, a2) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2) Source #

Methods

ix :: Index (a0, a1, a2) -> Optic' (IxKind (a0, a1, a2)) NoIx (a0, a1, a2) (IxValue (a0, a1, a2)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3) => Ixed (a0, a1, a2, a3) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3) Source #

Methods

ix :: Index (a0, a1, a2, a3) -> Optic' (IxKind (a0, a1, a2, a3)) NoIx (a0, a1, a2, a3) (IxValue (a0, a1, a2, a3)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4) => Ixed (a0, a1, a2, a3, a4) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4) Source #

Methods

ix :: Index (a0, a1, a2, a3, a4) -> Optic' (IxKind (a0, a1, a2, a3, a4)) NoIx (a0, a1, a2, a3, a4) (IxValue (a0, a1, a2, a3, a4)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5) => Ixed (a0, a1, a2, a3, a4, a5) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5) Source #

Methods

ix :: Index (a0, a1, a2, a3, a4, a5) -> Optic' (IxKind (a0, a1, a2, a3, a4, a5)) NoIx (a0, a1, a2, a3, a4, a5) (IxValue (a0, a1, a2, a3, a4, a5)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6) => Ixed (a0, a1, a2, a3, a4, a5, a6) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5, a6) Source #

Methods

ix :: Index (a0, a1, a2, a3, a4, a5, a6) -> Optic' (IxKind (a0, a1, a2, a3, a4, a5, a6)) NoIx (a0, a1, a2, a3, a4, a5, a6) (IxValue (a0, a1, a2, a3, a4, a5, a6)) Source #

(a0 ~ a1, a0 ~ a2, a0 ~ a3, a0 ~ a4, a0 ~ a5, a0 ~ a6, a0 ~ a7) => Ixed (a0, a1, a2, a3, a4, a5, a6, a7) Source # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5, a6, a7) Source #

Methods

ix :: Index (a0, a1, a2, a3, a4, a5, a6, a7) -> Optic' (IxKind (a0, a1, a2, a3, a4, a5, a6, a7)) NoIx (a0, a1, a2, a3, a4, a5, a6, a7) (IxValue (a0, a1, a2, a3, a4, a5, a6, a7)) Source #

(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 # 
Instance details

Defined in Optics.At.Core

Associated Types

type IxKind (a0, a1, a2, a3, a4, a5, a6, a7, a8) Source #

Methods

ix :: Index (a0, a1, a2, a3, a4, a5, a6, a7, a8) -> Optic' (IxKind (a0, a1, a2, a3, a4, a5, a6, a7, a8)) NoIx (a0, a1, a2, a3, a4, a5, a6, a7, a8) (IxValue (a0, a1, a2, a3, a4, a5, a6, a7, a8)) Source #

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

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

At

class (Ixed m, IxKind m ~ An_AffineTraversal) => 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

Methods

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.

Instances

Instances details
At IntSet Source # 
Instance details

Defined in Optics.At.Core

At (Maybe a) Source # 
Instance details

Defined in Optics.At.Core

Methods

at :: Index (Maybe a) -> Lens' (Maybe a) (Maybe (IxValue (Maybe a))) Source #

At (IntMap a) Source # 
Instance details

Defined in Optics.At.Core

Methods

at :: Index (IntMap a) -> Lens' (IntMap a) (Maybe (IxValue (IntMap a))) Source #

Ord k => At (Set k) Source # 
Instance details

Defined in Optics.At.Core

Methods

at :: Index (Set k) -> Lens' (Set k) (Maybe (IxValue (Set k))) Source #

Ord k => At (Map k a) Source # 
Instance details

Defined in Optics.At.Core

Methods

at :: Index (Map k a) -> Lens' (Map k a) (Maybe (IxValue (Map k a))) Source #

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

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

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

sans k = at k .~ Nothing

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.

Methods

contains :: Index m -> Lens' m Bool Source #

>>> 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 Source # 
Instance details

Defined in Optics.At.Core

Ord a => Contains (Set a) Source # 
Instance details

Defined in Optics.At.Core

Methods

contains :: Index (Set a) -> Lens' (Set a) Bool Source #