Copyright | (C) 2012-14 Edward Kmett |
---|---|

License | BSD-style (see the file LICENSE) |

Maintainer | Edward Kmett <ekmett@gmail.com> |

Stability | experimental |

Portability | non-portable |

Safe Haskell | Trustworthy |

Language | Haskell98 |

- 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 => Traversal' s a
- _tail :: Cons s s a a => Traversal' 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 => Traversal' s s
- _last :: Snoc s s a a => Traversal' s a

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

Nothing

Cons ByteString ByteString Word8 Word8 | |

Cons ByteString ByteString Word8 Word8 | |

Cons Text Text Char Char | |

Cons Text Text Char Char | |

Cons [a] [b] a b | |

Cons (Seq a) (Seq b) a b | |

Cons (Vector a) (Vector b) a b | |

(Prim a, Prim b) => Cons (Vector a) (Vector b) a b | |

(Storable a, Storable b) => Cons (Vector a) (Vector b) a b | |

(Unbox a, Unbox b) => Cons (Vector a) (Vector b) a b | |

Cons (Deque a) (Deque b) a b |

cons :: Cons s s a a => a -> s -> s infixr 5 Source

`cons`

an element onto a container.

`>>>`

[a]`cons a []`

`>>>`

[a,b,c]`cons a [b, c]`

`>>>`

fromList [a]`cons a (Seq.fromList [])`

`>>>`

fromList [a,b,c]`cons a (Seq.fromList [b, c])`

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 (a,[b,c])`uncons [a, b, c]`

_head :: Cons s s a a => Traversal' s a Source

A `Traversal`

reading and writing to the `head`

of a *non-empty* container.

`>>>`

Just a`[a,b,c]^? _head`

`>>>`

[d,b,c]`[a,b,c] & _head .~ d`

`>>>`

[f a,b,c]`[a,b,c] & _head %~ f`

`>>>`

[]`[] & _head %~ f`

`>>>`

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

`>>>`

Nothing`[]^?_head`

`>>>`

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

`>>>`

[]`[] & _head .~ 1`

`>>>`

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

`>>>`

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

This isn't limited to lists.

For instance you can also `traverse`

the head of a `Seq`

:

`>>>`

fromList [f a,b,c,d]`Seq.fromList [a,b,c,d] & _head %~ f`

`>>>`

Nothing`Seq.fromList [] ^? _head`

`>>>`

Just a`Seq.fromList [a,b,c,d] ^? _head`

`_head`

::`Traversal'`

[a] a`_head`

::`Traversal'`

(`Seq`

a) a`_head`

::`Traversal'`

(`Vector`

a) a

_tail :: Cons s s a a => Traversal' s s Source

A `Traversal`

reading and writing to the `tail`

of a *non-empty* container.

`>>>`

[a,c,d,e]`[a,b] & _tail .~ [c,d,e]`

`>>>`

[]`[] & _tail .~ [a,b]`

`>>>`

[a,f b,f c,f d,f e]`[a,b,c,d,e] & _tail.traverse %~ f`

`>>>`

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

`>>>`

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

`>>>`

Just [b,c]`[a,b,c]^?_tail`

`>>>`

[2]`[1,2]^?!_tail`

`>>>`

"ello"`"hello"^._tail`

`>>>`

""`""^._tail`

This isn't limited to lists. For instance you can also `traverse`

the tail of a `Seq`

.

`>>>`

fromList [a,c,d,e]`Seq.fromList [a,b] & _tail .~ Seq.fromList [c,d,e]`

`>>>`

Just (fromList [b,c])`Seq.fromList [a,b,c] ^? _tail`

`>>>`

Nothing`Seq.fromList [] ^? _tail`

`_tail`

::`Traversal'`

[a] [a]`_tail`

::`Traversal'`

(`Seq`

a) (`Seq`

a)`_tail`

::`Traversal'`

(`Vector`

a) (`Vector`

a)

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

Nothing

Snoc ByteString ByteString Word8 Word8 | |

Snoc ByteString ByteString Word8 Word8 | |

Snoc Text Text Char Char | |

Snoc Text Text Char Char | |

Snoc [a] [b] a b | |

Snoc (Seq a) (Seq b) a b | |

Snoc (Vector a) (Vector b) a b | |

(Prim a, Prim b) => Snoc (Vector a) (Vector b) a b | |

(Storable a, Storable b) => Snoc (Vector a) (Vector b) a b | |

(Unbox a, Unbox b) => Snoc (Vector a) (Vector b) a b | |

Snoc (Deque a) (Deque b) a b |

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

`snoc`

an element onto the end of a container.

`>>>`

fromList [a]`snoc (Seq.fromList []) a`

`>>>`

fromList [b,c,a]`snoc (Seq.fromList [b, c]) a`

`>>>`

"hello!"`snoc (LazyT.pack "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 (LazyT.pack "hello!")`

`>>>`

Nothing`unsnoc (LazyT.pack "")`

`>>>`

Just (fromList [b,c],a)`unsnoc (Seq.fromList [b,c,a])`

`>>>`

Nothing`unsnoc (Seq.fromList [])`

_init :: Snoc s s a a => Traversal' s s Source

A `Traversal`

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

`>>>`

Just [a,b,c]`[a,b,c,d]^?_init`

`>>>`

Nothing`[]^?_init`

`>>>`

[c,d,e,b]`[a,b] & _init .~ [c,d,e]`

`>>>`

[]`[] & _init .~ [a,b]`

`>>>`

[f a,f b,f c,d]`[a,b,c,d] & _init.traverse %~ f`

`>>>`

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

`>>>`

[1,2,3]`[1,2,3,4]^?!_init`

`>>>`

"hell"`"hello"^._init`

`>>>`

""`""^._init`

`_init`

::`Traversal'`

[a] [a]`_init`

::`Traversal'`

(`Seq`

a) (`Seq`

a)`_init`

::`Traversal'`

(`Vector`

a) (`Vector`

a)

_last :: Snoc s s a a => Traversal' s a Source

A `Traversal`

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

`>>>`

c`[a,b,c]^?!_last`

`>>>`

Nothing`[]^?_last`

`>>>`

[a,b,f c]`[a,b,c] & _last %~ f`

`>>>`

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

`>>>`

[]`[] & _last .~ 1`

`>>>`

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

`>>>`

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

This `Traversal`

is not limited to lists, however. We can also work with other containers, such as a `Vector`

.

`>>>`

Just 'e'`Vector.fromList "abcde" ^? _last`

`>>>`

Nothing`Vector.empty ^? _last`

`>>>`

fromList "abcdQ"`Vector.fromList "abcde" & _last .~ 'Q'`

`_last`

::`Traversal'`

[a] a`_last`

::`Traversal'`

(`Seq`

a) a`_last`

::`Traversal'`

(`Vector`

a) a