HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleoptics-extra-0.4.2.1Haskell2010

Optics.Cons

This module defines the Cons and Snoc classes, which provide Prisms for the leftmost and rightmost elements of a container, respectively.

  • 2 classes
  • 10 values

Cons

7 declarations
classclass Cons s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

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

Methods

Instances11Cons, …
value(<|) :: Cons s s a a => a -> s -> s
#

cons an element onto a container.

This is an infix alias for cons.

Example1 expression
1 <| [][1]
Example1 expression
'a' <| "bc""abc"
Example1 expression
1 <| [][1]
Example1 expression
1 <| [2, 3][1,2,3]
valuecons :: Cons s s a a => a -> s -> s
#

cons an element onto a container.

Example1 expression
cons 'a' """a"
Example1 expression
cons 'a' "bc""abc"
valueuncons :: Cons s s a a => s -> Maybe (a, s)
#

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

Example1 expression
uncons []Nothing
Example1 expression
uncons [1, 2, 3]Just (1,[2,3])
value_head :: Cons s s a a => AffineTraversal' s a
#

An AffineTraversal reading and writing to the head of a non-empty container.

Example1 expression
"abc" ^? _headJust 'a'
Example1 expression
"abc" & _head .~ 'd'"dbc"
Example1 expression
[1,2,3] & _head %~ (*10)[10,2,3]
Example1 expression
[] & _head %~ absurd[]
Example1 expression
[1,2,3] ^? _headJust 1
Example1 expression
[] ^? _headNothing
Example1 expression
[1,2] ^? _headJust 1
Example1 expression
[] & _head .~ 1[]
Example1 expression
[0] & _head .~ 2[2]
Example1 expression
[0,1] & _head .~ 2[2,1]
value_tail :: Cons s s a a => AffineTraversal' s s
#

An AffineTraversal reading and writing to the tail of a non-empty container.

Example1 expression
"ab" & _tail .~ "cde""acde"
Example1 expression
[] & _tail .~ [1,2][]
Example1 expression
[1,2,3,4,5] & _tail % traversed %~ (*10)[1,20,30,40,50]
Example1 expression
[1,2] & _tail .~ [3,4,5][1,3,4,5]
Example1 expression
[] & _tail .~ [1,2][]
Example1 expression
"abc" ^? _tailJust "bc"
Example1 expression
"hello" ^? _tailJust "ello"
Example1 expression
"" ^? _tailNothing
patternpattern (:<) :: Cons s s a a => a -> s -> s
#

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

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

Snoc

7 declarations
classclass Snoc s t a b | s -> a, t -> b, s b -> t, t a -> s where
#

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

Methods

Instances11Snoc, …
value(|>) :: Snoc s s a a => s -> a -> s
#

snoc an element onto the end of a container.

This is an infix alias for snoc.

Example1 expression
"" |> 'a'"a"
Example1 expression
"bc" |> 'a'"bca"
valuesnoc :: Snoc s s a a => s -> a -> s
#

snoc an element onto the end of a container.

Example1 expression
snoc "hello" '!'"hello!"
valueunsnoc :: Snoc s s a a => s -> Maybe (s, a)
#

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

Example1 expression
unsnoc "hello!"Just ("hello",'!')
Example1 expression
unsnoc ""Nothing
value_init :: Snoc s s a a => AffineTraversal' s s
#

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

Example1 expression
"abcd" ^? _initJust "abc"
Example1 expression
"" ^? _initNothing
Example1 expression
"ab" & _init .~ "cde""cdeb"
Example1 expression
[] & _init .~ [1,2][]
Example1 expression
[1,2,3,4] & _init % traversed %~ (*10)[10,20,30,4]
Example1 expression
[1,2,3] ^? _initJust [1,2]
Example1 expression
"hello" ^? _initJust "hell"
Example1 expression
[] ^? _initNothing
value_last :: Snoc s s a a => AffineTraversal' s a
#

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

Example1 expression
"abc" ^? _lastJust 'c'
Example1 expression
"" ^? _lastNothing
Example1 expression
[1,2,3] & _last %~ (+1)[1,2,4]
Example1 expression
[1,2] ^? _lastJust 2
Example1 expression
[] & _last .~ 1[]
Example1 expression
[0] & _last .~ 2[2]
Example1 expression
[0,1] & _last .~ 2[0,2]
patternpattern (:>) :: Snoc s s a a => s -> a -> s
#

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

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

Orphan instances

16 instances