optics-extra-0.1: Extra utilities and instances for optics-core

Safe HaskellNone
LanguageHaskell2010

Optics.At

Contents

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]

This module includes the core definitions from Optics.At.Core along with extra (orphan) instances.

Synopsis

Type families

type family Index s :: Type #

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

Defined in Optics.At

type Index ByteString Source # 
Instance details

Defined in Optics.At

type Index IntSet 
Instance details

Defined in Optics.At.Core

type Index Text Source # 
Instance details

Defined in Optics.At

type Index Text Source # 
Instance details

Defined in Optics.At

type Index Text = Int
type Index [a] 
Instance details

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

type Index (Set a) = a
type Index (HashSet a) Source # 
Instance details

Defined in Optics.At

type Index (HashSet a) = a
type Index (Vector a) Source # 
Instance details

Defined in Optics.At

type Index (Vector a) = Int
type Index (Vector a) Source # 
Instance details

Defined in Optics.At

type Index (Vector a) = Int
type Index (Vector a) Source # 
Instance details

Defined in Optics.At

type Index (Vector a) = Int
type Index (Vector a) Source # 
Instance details

Defined in Optics.At

type Index (Vector a) = Int
type Index (e -> a) 
Instance details

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

type Index (Map k a) = k
type Index (HashMap k a) Source # 
Instance details

Defined in Optics.At

type Index (HashMap k a) = k
type Index (a, b, c) 
Instance details

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

type Index (a, b, c, d, e) = Int
type Index (a, b, c, d, e, f) 
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) 
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) 
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) 
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 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
type IxValue ByteString Source # 
Instance details

Defined in Optics.At

type IxValue ByteString Source # 
Instance details

Defined in Optics.At

type IxValue IntSet 
Instance details

Defined in Optics.At.Core

type IxValue IntSet = ()
type IxValue Text Source # 
Instance details

Defined in Optics.At

type IxValue Text Source # 
Instance details

Defined in Optics.At

type IxValue [a] 
Instance details

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

type IxValue (Set k) = ()
type IxValue (HashSet k) Source # 
Instance details

Defined in Optics.At

type IxValue (HashSet k) = ()
type IxValue (Vector a) Source # 
Instance details

Defined in Optics.At

type IxValue (Vector a) = a
type IxValue (Vector a) Source # 
Instance details

Defined in Optics.At

type IxValue (Vector a) = a
type IxValue (Vector a) Source # 
Instance details

Defined in Optics.At

type IxValue (Vector a) = a
type IxValue (Vector a) Source # 
Instance details

Defined in Optics.At

type IxValue (Vector a) = a
type IxValue (e -> a) 
Instance details

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

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

Defined in Optics.At.Core

type IxValue (Map k a) = a
type IxValue (HashMap k a) Source # 
Instance details

Defined in Optics.At

type IxValue (HashMap k a) = a
type IxValue (a0, a1, a2)
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)
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)
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)
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)
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)
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)
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 #

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

Methods

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

Defined in Optics.At

Ixed ByteString Source # 
Instance details

Defined in Optics.At

Ixed IntSet 
Instance details

Defined in Optics.At.Core

Ixed Text Source # 
Instance details

Defined in Optics.At

Ixed Text Source # 
Instance details

Defined in Optics.At

Ixed [a] 
Instance details

Defined in Optics.At.Core

Methods

ix :: Index [a] -> AffineTraversal' [a] (IxValue [a]) #

Ixed (Maybe a) 
Instance details

Defined in Optics.At.Core

Methods

ix :: Index (Maybe a) -> AffineTraversal' (Maybe a) (IxValue (Maybe a)) #

Ixed (Identity a) 
Instance details

Defined in Optics.At.Core

Ixed (NonEmpty a) 
Instance details

Defined in Optics.At.Core

Ixed (IntMap a) 
Instance details

Defined in Optics.At.Core

Methods

ix :: Index (IntMap a) -> AffineTraversal' (IntMap a) (IxValue (IntMap a)) #

Ixed (Tree a) 
Instance details

Defined in Optics.At.Core

Methods

ix :: Index (Tree a) -> AffineTraversal' (Tree a) (IxValue (Tree a)) #

Ixed (Seq a) 
Instance details

Defined in Optics.At.Core

Methods

ix :: Index (Seq a) -> AffineTraversal' (Seq a) (IxValue (Seq a)) #

Ord k => Ixed (Set k) 
Instance details

Defined in Optics.At.Core

Methods

ix :: Index (Set k) -> AffineTraversal' (Set k) (IxValue (Set k)) #

(Eq k, Hashable k) => Ixed (HashSet k) Source # 
Instance details

Defined in Optics.At

Unbox a => Ixed (Vector a) Source # 
Instance details

Defined in Optics.At

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

Storable a => Ixed (Vector a) Source # 
Instance details

Defined in Optics.At

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

Prim a => Ixed (Vector a) Source # 
Instance details

Defined in Optics.At

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

Ixed (Vector a) Source # 
Instance details

Defined in Optics.At

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

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

Defined in Optics.At.Core

Methods

ix :: Index (e -> a) -> AffineTraversal' (e -> a) (IxValue (e -> a)) #

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

Defined in Optics.At.Core

Methods

ix :: Index (a0, a1) -> AffineTraversal' (a0, a1) (IxValue (a0, a1)) #

(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 Optics.At.Core

Methods

ix :: Index (UArray i e) -> AffineTraversal' (UArray i e) (IxValue (UArray i e)) #

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

Defined in Optics.At.Core

Methods

ix :: Index (Array i e) -> AffineTraversal' (Array i e) (IxValue (Array i e)) #

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

Defined in Optics.At.Core

Methods

ix :: Index (Map k a) -> AffineTraversal' (Map k a) (IxValue (Map k a)) #

(Eq k, Hashable k) => Ixed (HashMap k a) Source # 
Instance details

Defined in Optics.At

Methods

ix :: Index (HashMap k a) -> AffineTraversal' (HashMap k a) (IxValue (HashMap k a)) #

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

Defined in Optics.At.Core

Methods

ix :: Index (a0, a1, a2) -> AffineTraversal' (a0, a1, a2) (IxValue (a0, a1, a2)) #

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

Defined in Optics.At.Core

Methods

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) 
Instance details

Defined in Optics.At.Core

Methods

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) 
Instance details

Defined in Optics.At.Core

Methods

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) 
Instance details

Defined in Optics.At.Core

Methods

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) 
Instance details

Defined in Optics.At.Core

Methods

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) 
Instance details

Defined in Optics.At.Core

Methods

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

ixAt :: At m => Index m -> AffineTraversal' 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.

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 % _Just

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: 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
At IntSet 
Instance details

Defined in Optics.At.Core

At (Maybe a) 
Instance details

Defined in Optics.At.Core

Methods

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

At (IntMap a) 
Instance details

Defined in Optics.At.Core

Methods

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

Ord k => At (Set k) 
Instance details

Defined in Optics.At.Core

Methods

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

(Eq k, Hashable k) => At (HashSet k) Source # 
Instance details

Defined in Optics.At

Methods

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

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

Defined in Optics.At.Core

Methods

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

(Eq k, Hashable k) => At (HashMap k a) Source # 
Instance details

Defined in Optics.At

Methods

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

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

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

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

sans k = at k .~ 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. Instances are provided for Set-like containers only.

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
Contains IntSet 
Instance details

Defined in Optics.At.Core

Ord a => Contains (Set a) 
Instance details

Defined in Optics.At.Core

Methods

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

(Eq a, Hashable a) => Contains (HashSet a) Source # 
Instance details

Defined in Optics.At

Methods

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

Orphan instances

Ixed ByteString Source # 
Instance details

Ixed ByteString Source # 
Instance details

Ixed Text Source # 
Instance details

Ixed Text Source # 
Instance details

(Eq a, Hashable a) => Contains (HashSet a) Source # 
Instance details

Methods

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

(Eq k, Hashable k) => Ixed (HashSet k) Source # 
Instance details

Unbox a => Ixed (Vector a) Source # 
Instance details

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

Storable a => Ixed (Vector a) Source # 
Instance details

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

Prim a => Ixed (Vector a) Source # 
Instance details

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

Ixed (Vector a) Source # 
Instance details

Methods

ix :: Index (Vector a) -> AffineTraversal' (Vector a) (IxValue (Vector a)) #

(Eq k, Hashable k) => At (HashSet k) Source # 
Instance details

Methods

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

(Eq k, Hashable k) => Ixed (HashMap k a) Source # 
Instance details

Methods

ix :: Index (HashMap k a) -> AffineTraversal' (HashMap k a) (IxValue (HashMap k a)) #

(Eq k, Hashable k) => At (HashMap k a) Source # 
Instance details

Methods

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