lens-4.2: Lenses, Folds and Traversals

Copyright(C) 2012-14 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell98

Control.Lens.At

Contents

Description

 

Synopsis

At

class Ixed m => 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 . traverse

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

Instances

At IntSet 
At (Maybe a) 
At (IntMap a) 
Ord k => At (Set k) 
(Eq k, Hashable k) => At (HashSet k) 
(Eq k, Hashable k) => At (HashMap k a) 
Ord k => At (Map k a) 

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

Ixed

type family Index s :: * Source

Instances

type Index ByteString = Int 
type Index ByteString = Int64 
type Index Text = Int 
type Index Value = Text 
type Index IntSet = Int 
type Index Text = Int64 
type Index [a] = Int 
type Index (Maybe a) = () 
type Index (Complex a) = Int 
type Index (Identity a) = () 
type Index (IntMap a) = Int 
type Index (Set a) = a 
type Index (Tree a) = [Int] 
type Index (Seq a) = Int 
type Index (NonEmpty a) = Int 
type Index (HashSet a) = a 
type Index (Vector a) = Int 
type Index (Vector a) = Int 
type Index (Vector a) = Int 
type Index (Vector a) = Int 
type Index (e -> a) = e 
type Index (a, b) = Int 
type Index (a, b) = Int 
type Index (HashMap k a) = k 
type Index (Map k a) = k 
type Index (UArray i e) = i 
type Index (Array i e) = i 
type Index (a, b, c) = Int 
type Index (a, b, c) = Int 
type Index (a, b, c, d) = Int 
type Index (a, b, c, d) = Int 
type Index (a, b, c, d, e) = Int 
type Index (a, b, c, d, e) = Int 
type Index (a, b, c, d, e, f) = Int 
type Index (a, b, c, d, e, f) = Int 
type Index (a, b, c, d, e, f, g) = Int 
type Index (a, b, c, d, e, f, g) = Int 
type Index (a, b, c, d, e, f, g, h) = Int 
type Index (a, b, c, d, e, f, g, h) = Int 
type Index (a, b, c, d, e, f, g, h, i) = Int 
type Index (a, b, c, d, e, f, g, h, i) = Int 

type family IxValue m :: * Source

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

Instances

type IxValue ByteString = Word8 
type IxValue ByteString = Word8 
type IxValue Text = Char 
type IxValue Value = Value 
type IxValue IntSet = () 
type IxValue Text = Char 
type IxValue [a] = a 
type IxValue (Maybe a) = a 
type IxValue (Identity a) = a 
type IxValue (IntMap a) = a 
type IxValue (Set k) = () 
type IxValue (Tree a) = a 
type IxValue (Seq a) = a 
type IxValue (NonEmpty a) = a 
type IxValue (HashSet k) = () 
type IxValue (Vector a) = a 
type IxValue (Vector a) = a 
type IxValue (Vector a) = a 
type IxValue (Vector a) = a 
type IxValue (e -> a) = a 
type IxValue (a, b) = a 
type IxValue (HashMap k a) = a 
type IxValue (Map k a) = a 
type IxValue (UArray i e) = e 
type IxValue (Array i e) = e 
type IxValue (a, b, c) = a 
type IxValue (a, b, c, d) = a 
type IxValue (a, b, c, d, e) = a 
type IxValue (a, b, c, d, e, f) = a 
type IxValue (a, b, c, d, e, f, g) = a 
type IxValue (a, b, c, d, e, f, g, h) = a 
type IxValue (a, b, c, d, e, f, g, h, i) = a 

class Ixed m where Source

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

Methods

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

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

NB: Setting the value of this AffineTraversal will only set the value in the Lens 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

Instances

Ixed ByteString 
Ixed ByteString 
Ixed Text 
Ixed Value 
Ixed IntSet 
Ixed Text 
Ixed [a] 
Ixed (Maybe a) 
Ixed (Identity a) 
Ixed (IntMap a) 
Ord k => Ixed (Set k) 
Ixed (Tree a) 
Ixed (Seq a) 
Ixed (NonEmpty a) 
(Eq k, Hashable k) => Ixed (HashSet k) 
Ixed (Vector a) 
Unbox a => Ixed (Vector a) 
Storable a => Ixed (Vector a) 
Prim a => Ixed (Vector a) 
Eq e => Ixed (e -> a) 
(~) * a b => Ixed (a, b) 
(Eq k, Hashable k) => Ixed (HashMap k a) 
Ord k => Ixed (Map k a) 
(IArray UArray e, Ix i) => Ixed (UArray i e)
arr ! i ≡ arr 'Control.Lens.Getter.^.' ix i
arr // [(i,e)] ≡ ix i .~ e $ arr
Ix i => Ixed (Array i e)
arr ! i ≡ arr 'Control.Lens.Getter.^.' ix i
arr // [(i,e)] ≡ ix i .~ e $ arr
((~) * a b, (~) * a c) => Ixed (a, b, c) 
((~) * a b, (~) * a c, (~) * a d) => Ixed (a, b, c, d) 
((~) * a b, (~) * a c, (~) * a d, (~) * a e) => Ixed (a, b, c, d, e) 
((~) * a b, (~) * a c, (~) * a d, (~) * a e, (~) * a f) => Ixed (a, b, c, d, e, f) 
((~) * a b, (~) * a c, (~) * a d, (~) * a e, (~) * a f, (~) * a g) => Ixed (a, b, c, d, e, f, g) 
((~) * a b, (~) * a c, (~) * a d, (~) * a e, (~) * a f, (~) * a g, (~) * a h) => Ixed (a, b, c, d, e, f, g, h) 
((~) * a b, (~) * a c, (~) * a d, (~) * a e, (~) * a f, (~) * a g, (~) * a h, (~) * a i) => Ixed (a, b, c, d, e, f, g, h, i) 

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

Contains

class Contains m where Source

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

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