lens-3.7.4: Lenses, Folds and Traversals

Portability non-portable experimental Edward Kmett None

Data.Sequence.Lens

Description

Synopsis

# Documentation

A Lens that can access the nth element of a Seq.

>>> Seq.fromList [a,b,c,d] & ordinal 2 %~ f
fromList [a,b,f c,d]
>>> Seq.fromList [a,b,c,d] & ordinal 2 .~ e
fromList [a,b,e,d]
>>> Seq.fromList [a,b,c,d] ^. ordinal 2
c
• NB:* This is only a legal lens if there is already such an element!

viewL :: Iso (Seq a) (Seq b) (ViewL a) (ViewL b)Source

A Seq is isomorphic to a ViewL

viewl m ≡ m ^. viewL
>>> Seq.fromList [a,b,c] ^. viewL
a :< fromList [b,c]
>>> Seq.empty ^. viewL
EmptyL
>>> EmptyL ^. from viewL
fromList []
>>> from viewL ^\$ a :< fromList [b,c]
fromList [a,b,c]

viewR :: Iso (Seq a) (Seq b) (ViewR a) (ViewR b)Source

A Seq is isomorphic to a ViewR

viewr m ≡ m ^. viewR
>>> Seq.fromList [a,b,c] ^. viewR
fromList [a,b] :> c
>>> Seq.empty ^. viewR
EmptyR
>>> EmptyR ^. from viewR
fromList []
>>> from viewR ^\$ fromList [a,b] :> c
fromList [a,b,c]

Traverse the head of a Seq

>>> fromList [a,b,c,d] & _head %~ f
fromList [f a,b,c,d]
Nothing
Just a

_tail :: SimpleTraversal (Seq a) (Seq a)Source

Traverse the tail of a Seq

>>> fromList [a,b] & _tail .~ fromList [c,d,e]
fromList [a,c,d,e]
>>> fromList [a,b,c] ^? _tail
Just (fromList [b,c])
>>> fromList [] ^? _tail
Nothing

Traverse the last element of a Seq

>>> fromList [a,b,c,d] & _last %~ f
fromList [a,b,c,f d]
>>> fromList [a,b,c,d] ^? _last
Just d
>>> fromList [] ^? _last
Nothing

_init :: SimpleTraversal (Seq a) (Seq a)Source

Traverse all but the last element of a Seq

>>> fromList [1,2,3] ^? _init
Just (fromList [1,2])
>>> fromList [a,b,c,d] & _init.traverse %~ f
fromList [f a,f b,f c,d]
>>> fromList [] & _init .~ fromList [a,b,c]
fromList []

Traverse all the elements numbered from i to j of a Seq

>>> fromList [a,b,c,d,e] & sliced 1 3 %~ f
fromList [a,f b,f c,d,e]

Traverse the first n elements of a Seq

>>> fromList [a,b,c,d,e] ^.. slicedTo 2
[a,b]
>>> fromList [a,b,c,d,e] & slicedTo 2 %~ f
fromList [f a,f b,c,d,e]
>>> fromList [a,b,c,d,e] & slicedTo 10 .~ x
fromList [x,x,x,x,x]

Traverse all but the first n elements of a Seq

>>> fromList [a,b,c,d,e] ^.. slicedFrom 2
[c,d,e]
>>> fromList [a,b,c,d,e] & slicedFrom 2 %~ f
fromList [a,b,f c,f d,f e]
>>> fromList [a,b,c,d,e] & slicedFrom 10 .~ x
fromList [a,b,c,d,e]