Safe Haskell | None |
---|---|

Language | Haskell2010 |

This module defines the `Cons`

and `Snoc`

classes, which provide `Prism`

s for
the leftmost and rightmost elements of a container, respectively.

Note that orphan instances for these classes are defined in the `Optics.Cons`

module from `optics-extra`

, so if you are not simply depending on `optics`

you may wish to import that module instead.

## Synopsis

- class Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where
- (<|) :: Cons s s a a => a -> s -> s
- cons :: Cons s s a a => a -> s -> s
- uncons :: Cons s s a a => s -> Maybe (a, s)
- _head :: Cons s s a a => AffineTraversal' s a
- _tail :: Cons s s a a => AffineTraversal' s s
- pattern (:<) :: forall s a. Cons s s a a => a -> s -> s
- class Snoc s t a b | s -> a, t -> b, s b -> t, t a -> s where
- (|>) :: Snoc s s a a => s -> a -> s
- snoc :: Snoc s s a a => s -> a -> s
- unsnoc :: Snoc s s a a => s -> Maybe (s, a)
- _init :: Snoc s s a a => AffineTraversal' s s
- _last :: Snoc s s a a => AffineTraversal' s a
- pattern (:>) :: forall s a. Snoc s s a a => s -> a -> s

# Cons

class Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where Source #

This class provides a way to attach or detach elements on the left side of a structure in a flexible manner.

uncons :: Cons s s a a => s -> Maybe (a, s) Source #

Attempt to extract the left-most element from a container, and a version of the container without that element.

`>>>`

Nothing`uncons []`

`>>>`

Just (1,[2,3])`uncons [1, 2, 3]`

_head :: Cons s s a a => AffineTraversal' s a Source #

An `AffineTraversal`

reading and writing to the `head`

of a *non-empty*
container.

`>>>`

Just 'a'`"abc" ^? _head`

`>>>`

"dbc"`"abc" & _head .~ 'd'`

`>>>`

[10,2,3]`[1,2,3] & _head %~ (*10)`

`>>>`

[]`[] & _head %~ absurd`

`>>>`

Just 1`[1,2,3] ^? _head`

`>>>`

Nothing`[] ^? _head`

`>>>`

Just 1`[1,2] ^? _head`

`>>>`

[]`[] & _head .~ 1`

`>>>`

[2]`[0] & _head .~ 2`

`>>>`

[2,1]`[0,1] & _head .~ 2`

_tail :: Cons s s a a => AffineTraversal' s s Source #

An `AffineTraversal`

reading and writing to the `tail`

of a *non-empty*
container.

`>>>`

"acde"`"ab" & _tail .~ "cde"`

`>>>`

[]`[] & _tail .~ [1,2]`

`>>>`

[1,20,30,40,50]`[1,2,3,4,5] & _tail % traversed %~ (*10)`

`>>>`

[1,3,4,5]`[1,2] & _tail .~ [3,4,5]`

`>>>`

[]`[] & _tail .~ [1,2]`

`>>>`

Just "bc"`"abc" ^? _tail`

`>>>`

Just "ello"`"hello" ^? _tail`

`>>>`

Nothing`"" ^? _tail`

pattern (:<) :: forall s a. Cons s s a a => a -> s -> s infixr 5 Source #

Pattern synonym for matching on the leftmost element of a structure.

`>>>`

'a'`case ['a','b','c'] of (x :< _) -> x`

# Snoc

class Snoc s t a b | s -> a, t -> b, s b -> t, t a -> s where Source #

This class provides a way to attach or detach elements on the right side of a structure in a flexible manner.

snoc :: Snoc s s a a => s -> a -> s infixl 5 Source #

`snoc`

an element onto the end of a container.

`>>>`

"hello!"`snoc "hello" '!'`

unsnoc :: Snoc s s a a => s -> Maybe (s, a) Source #

Attempt to extract the right-most element from a container, and a version of the container without that element.

`>>>`

Just ("hello",'!')`unsnoc "hello!"`

`>>>`

Nothing`unsnoc ""`

_init :: Snoc s s a a => AffineTraversal' s s Source #

An `AffineTraversal`

reading and replacing all but the a last element of a
*non-empty* container.

`>>>`

Just "abc"`"abcd" ^? _init`

`>>>`

Nothing`"" ^? _init`

`>>>`

"cdeb"`"ab" & _init .~ "cde"`

`>>>`

[]`[] & _init .~ [1,2]`

`>>>`

[10,20,30,4]`[1,2,3,4] & _init % traversed %~ (*10)`

`>>>`

Just [1,2]`[1,2,3] ^? _init`

`>>>`

Just "hell"`"hello" ^? _init`

`>>>`

Nothing`[] ^? _init`

_last :: Snoc s s a a => AffineTraversal' s a Source #

An `AffineTraversal`

reading and writing to the last element of a
*non-empty* container.

`>>>`

Just 'c'`"abc" ^? _last`

`>>>`

Nothing`"" ^? _last`

`>>>`

[1,2,4]`[1,2,3] & _last %~ (+1)`

`>>>`

Just 2`[1,2] ^? _last`

`>>>`

[]`[] & _last .~ 1`

`>>>`

[2]`[0] & _last .~ 2`

`>>>`

[0,2]`[0,1] & _last .~ 2`