Additional optics for manipulating lists are present more
generically in this package.
The Ixed class allows traversing the element at a
specific list index.
Example1 expression
>>> [0..10] ^? ix 4Just 4
Example1 expression
>>> [0..5] & ix 4 .~ 2[0,1,2,3,2,5]
Example1 expression
>>> [0..10] ^? ix 14Nothing
Example1 expression
>>> [0..5] & ix 14 .~ 2[0,1,2,3,4,5]
The Cons and AsEmpty
classes provide Control.Lens.Prism.Prisms for list constructors.
Example1 expression
>>> [1..10] ^? _ConsJust (1,[2,3,4,5,6,7,8,9,10])
Example1 expression
>>> [] ^? _ConsNothing
Example1 expression
>>> [] ^? _EmptyJust ()
Example1 expression
>>> _Cons # (1, _Empty # ()) :: [Int][1]
Additionally, Snoc provides a
Control.Lens.Prism.Prism for accessing the end of a list. Note
that this Control.Lens.Prism.Prism always will need to traverse
the whole list.
Example1 expression
>>> [1..5] ^? _SnocJust ([1,2,3,4],5)
Example1 expression
>>> _Snoc # ([1,2],5)[1,2,5]
An instance of Plated allows for finding
locations in the list where a traversal matches.
Example1 expression
>>> [Nothing, Just 7, Just 3, Nothing] & deep (ix 0 . _Just) +~ 10[Nothing,Just 17,Just 3,Nothing]
An instance of Control.Lens.Iso.Reversing provides an
Control.Lens.Iso.Iso between a list and its reverse.
Example1 expression
>>> "live" & reversed %~ ('d':)"lived"
It's possible to work under a prefix or suffix of a list using
Prefixed and Suffixed.
Example1 expression
>>> "preview" ^? prefixed "pre"Just "view"
Example1 expression
>>> suffixed ".o" # "hello""hello.o"
At present, Data.List.Lens re-exports Prefixed and Suffixed for
backwards compatibility, as prefixed and suffixed used to be top-level
functions defined in this module. This may change in a future major release
of lens.
\mathcal{O}(\min(m,n)). The stripSuffix function drops the given
suffix from a list. It returns Nothing if the list did not end with the
suffix given, or Just the list after the suffix, if it does.