lens-4.12.3: Lenses, Folds and Traversals

Copyright(C) 2012-15 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 Source 
At (Maybe a) Source 

Methods

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

At (IntMap a) Source 

Methods

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

Ord k => At (Set k) Source 

Methods

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

(Eq k, Hashable k) => At (HashSet k) Source 

Methods

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

Ord k => At (Map k a) Source 

Methods

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

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

Methods

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

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

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

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 :: * Source

Instances

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

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 Source 
type IxValue ByteString = Word8 Source 
type IxValue IntSet = () Source 
type IxValue Text = Char Source 
type IxValue Text = Char Source 
type IxValue [a] = a Source 
type IxValue (Identity a) = a Source 
type IxValue (Maybe a) = a Source 
type IxValue (IntMap a) = a Source 
type IxValue (Set k) = () Source 
type IxValue (Tree a) = a Source 
type IxValue (Seq a) = a Source 
type IxValue (NonEmpty a) = a Source 
type IxValue (HashSet k) = () Source 
type IxValue (Vector a) = a Source 
type IxValue (Vector a) = a Source 
type IxValue (Vector a) = a Source 
type IxValue (Vector a) = a Source 
type IxValue (e -> a) = a Source 
type IxValue (a, a2) = a Source
ix :: Int -> Traversal' (a,a) a
type IxValue (UArray i e) = e Source 
type IxValue (Array i e) = e Source 
type IxValue (Map k a) = a Source 
type IxValue (HashMap k a) = a Source 
type IxValue (a, a2, a3) = a Source
ix :: Int -> Traversal' (a,a,a) a
type IxValue (a, a2, a3, a4) = a Source
ix :: Int -> Traversal' (a,a,a,a) a
type IxValue (a, a2, a3, a4, a5) = a Source
ix :: Int -> Traversal' (a,a,a,a,a) a
type IxValue (a, a2, a3, a4, a5, a6) = a Source
ix :: Int -> Traversal' (a,a,a,a,a,a) a
type IxValue (a, a2, a3, a4, a5, a6, a7) = a Source
ix :: Int -> Traversal' (a,a,a,a,a,a,a) a
type IxValue (a, a2, a3, a4, a5, a6, a7, a8) = a Source
ix :: Int -> Traversal' (a,a,a,a,a,a,a,a) a
type IxValue (a, a2, a3, a4, a5, a6, a7, a8, a9) = a Source
ix :: Int -> Traversal' (a,a,a,a,a,a,a,a,a) a

class Ixed m where Source

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

Methods

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

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

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

Instances

Ixed ByteString Source 
Ixed ByteString Source 
Ixed IntSet Source 
Ixed Text Source 
Ixed Text Source 
Ixed [a] Source 

Methods

ix :: Index [a] -> Traversal' [a] (IxValue [a]) Source

Ixed (Identity a) Source 
Ixed (Maybe a) Source 

Methods

ix :: Index (Maybe a) -> Traversal' (Maybe a) (IxValue (Maybe a)) Source

Ixed (IntMap a) Source 

Methods

ix :: Index (IntMap a) -> Traversal' (IntMap a) (IxValue (IntMap a)) Source

Ord k => Ixed (Set k) Source 

Methods

ix :: Index (Set k) -> Traversal' (Set k) (IxValue (Set k)) Source

Ixed (Tree a) Source 

Methods

ix :: Index (Tree a) -> Traversal' (Tree a) (IxValue (Tree a)) Source

Ixed (Seq a) Source 

Methods

ix :: Index (Seq a) -> Traversal' (Seq a) (IxValue (Seq a)) Source

Ixed (NonEmpty a) Source 
(Eq k, Hashable k) => Ixed (HashSet k) Source 

Methods

ix :: Index (HashSet k) -> Traversal' (HashSet k) (IxValue (HashSet k)) Source

Ixed (Vector a) Source 

Methods

ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) Source

Unbox a => Ixed (Vector a) Source 

Methods

ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) Source

Storable a => Ixed (Vector a) Source 

Methods

ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) Source

Prim a => Ixed (Vector a) Source 

Methods

ix :: Index (Vector a) -> Traversal' (Vector a) (IxValue (Vector a)) Source

Eq e => Ixed (e -> a) Source 

Methods

ix :: Index (e -> a) -> Traversal' (e -> a) (IxValue (e -> a)) Source

(~) * a a2 => Ixed (a, a2) Source 

Methods

ix :: Index (a, a2) -> Traversal' (a, a2) (IxValue (a, a2)) Source

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

Methods

ix :: Index (UArray i e) -> Traversal' (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

Methods

ix :: Index (Array i e) -> Traversal' (Array i e) (IxValue (Array i e)) Source

Ord k => Ixed (Map k a) Source 

Methods

ix :: Index (Map k a) -> Traversal' (Map k a) (IxValue (Map k a)) Source

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

Methods

ix :: Index (HashMap k a) -> Traversal' (HashMap k a) (IxValue (HashMap k a)) Source

((~) * a a2, (~) * a a3) => Ixed (a, a2, a3) Source 

Methods

ix :: Index (a, a2, a3) -> Traversal' (a, a2, a3) (IxValue (a, a2, a3)) Source

((~) * a a2, (~) * a a3, (~) * a a4) => Ixed (a, a2, a3, a4) Source 

Methods

ix :: Index (a, a2, a3, a4) -> Traversal' (a, a2, a3, a4) (IxValue (a, a2, a3, a4)) Source

((~) * a a2, (~) * a a3, (~) * a a4, (~) * a a5) => Ixed (a, a2, a3, a4, a5) Source 

Methods

ix :: Index (a, a2, a3, a4, a5) -> Traversal' (a, a2, a3, a4, a5) (IxValue (a, a2, a3, a4, a5)) Source

((~) * a a2, (~) * a a3, (~) * a a4, (~) * a a5, (~) * a a6) => Ixed (a, a2, a3, a4, a5, a6) Source 

Methods

ix :: Index (a, a2, a3, a4, a5, a6) -> Traversal' (a, a2, a3, a4, a5, a6) (IxValue (a, a2, a3, a4, a5, a6)) Source

((~) * a a2, (~) * a a3, (~) * a a4, (~) * a a5, (~) * a a6, (~) * a a7) => Ixed (a, a2, a3, a4, a5, a6, a7) Source 

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

((~) * a a2, (~) * a a3, (~) * a a4, (~) * a a5, (~) * a a6, (~) * a a7, (~) * a a8) => Ixed (a, a2, a3, a4, a5, a6, a7, a8) Source 

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

((~) * 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) Source 

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

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.

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

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 Source

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

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

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]