lens-3.3: Lenses, Folds and Traversals

Portabilityportable
Stabilityprovisional
MaintainerEdward Kmett <ekmett@gmail.com>
Safe HaskellSafe-Inferred

Data.List.Lens

Contents

Description

Traversals for manipulating parts of a list.

Synopsis

Partial Lenses

_head :: Simple Lens [a] aSource

A partial Lens reading and writing to the head of a non-empty list.

Attempting to read or write to the head of an empty list will result in an error.

>>> [1,2,3]^._head
1

_tail :: Simple Lens [a] [a]Source

A partial Lens reading and writing to the tail of a non-empty list

Attempting to read or write to the tail of an empty list will result in an error.

>>> _tail .~ [3,4,5] $ [1,2]
[1,3,4,5]

_last :: Simple Lens [a] aSource

A partial Lens reading and writing to the last element of a non-empty list

Attempting to read or write to the last element of an empty list will result in an error.

>>> [1,2]^._last
2

_init :: Simple Lens [a] [a]Source

A partial Lens reading and replacing all but the a last element of a non-empty list

Attempting to read or write to all but the last element of an empty list will result in an error.

>>> [1,2,3,4]^._init
[1,2,3]

Traversals

traverseHead :: SimpleIndexedTraversal Int [a] aSource

A traversal for reading and writing to the head of a list

The position of the head in the original list (0) is available as the index.

>>> traverseHead +~ 1 $ [1,2,3]
[2,2,3]
traverseHead :: Applicative f => (a -> f a) -> [a] -> f [a]

traverseTail :: SimpleIndexedTraversal Int [a] aSource

A traversal for editing the tail of a list

The position of each element in the original list is available as the index.

>>> traverseTail +~ 1 $ [1,2,3]
[1,3,4]
traverseTail :: Applicative f => (a -> f a) -> [a] -> f [a]

traverseInit :: SimpleIndexedTraversal Int [a] aSource

A traversal of all but the last element of a list

The position of each element is available as the index.

>>> traverseInit +~ 1 $ [1,2,3]
[2,3,3]
traverseInit :: Applicative f => (a -> f a) -> [a] -> f [a]

traverseLast :: SimpleIndexedTraversal Int [a] aSource

A traversal the last element in a list

The position of the last element in the original list is available as the index.

>>> traverseLast +~ 1 $ [1,2,3]
[1,2,4]
traverseLast :: Applicative f => (a -> f a) -> [a] -> f [a]