HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelens-5.3.5Haskell2010

Control.Lens.Cons

  • 2 classes
  • 22 values
  • Packagelens-5.3.5
  • Exports26
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceCons.hs

Cons

13 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

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

cons an element onto a container.

This is an infix alias for cons.

Example1 expression
a <| [][a]
Example1 expression
a <| [b, c][a,b,c]
Example1 expression
a <| Seq.fromList []fromList [a]
Example1 expression
a <| Seq.fromList [b, c]fromList [a,b,c]
valuecons :: Cons s s a a => a -> s -> s
#

cons an element onto a container.

Example1 expression
cons a [][a]
Example1 expression
cons a [b, c][a,b,c]
Example1 expression
cons a (Seq.fromList [])fromList [a]
Example1 expression
cons a (Seq.fromList [b, c])fromList [a,b,c]
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 [a, b, c]Just (a,[b,c])
value_head :: Cons s s a a => Traversal' s a
#

A Traversal reading and writing to the head of a non-empty container.

Example1 expression
[a,b,c]^? _headJust a
Example1 expression
[a,b,c] & _head .~ d[d,b,c]
Example1 expression
[a,b,c] & _head %~ f[f a,b,c]
Example1 expression
[] & _head %~ f[]
Example1 expression
[1,2,3]^?!_head1
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]

This isn't limited to lists.

For instance you can also traverse the head of a Seq:

Example1 expression
Seq.fromList [a,b,c,d] & _head %~ ffromList [f a,b,c,d]
Example1 expression
Seq.fromList [] ^? _headNothing
Example1 expression
Seq.fromList [a,b,c,d] ^? _headJust a
_head :: Traversal' [a] a
_head :: Traversal' (Seq a) a
_head :: Traversal' (Vector a) a
value_tail :: Cons s s a a => Traversal' s s
#

A Traversal reading and writing to the tail of a non-empty container.

Example1 expression
[a,b] & _tail .~ [c,d,e][a,c,d,e]
Example1 expression
[] & _tail .~ [a,b][]
Example1 expression
[a,b,c,d,e] & _tail.traverse %~ f[a,f b,f c,f d,f e]
Example1 expression
[1,2] & _tail .~ [3,4,5][1,3,4,5]
Example1 expression
[] & _tail .~ [1,2][]
Example1 expression
[a,b,c]^?_tailJust [b,c]
Example1 expression
[1,2]^?!_tail[2]
Example1 expression
"hello"^._tail"ello"
Example1 expression
""^._tail""

This isn't limited to lists. For instance you can also Control.Traversable.traverse the tail of a Seq.

Example1 expression
Seq.fromList [a,b] & _tail .~ Seq.fromList [c,d,e]fromList [a,c,d,e]
Example1 expression
Seq.fromList [a,b,c] ^? _tailJust (fromList [b,c])
Example1 expression
Seq.fromList [] ^? _tailNothing
_tail :: Traversal' [a] [a]
_tail :: Traversal' (Seq a) (Seq a)
_tail :: Traversal' (Vector a) (Vector a)
value(<|~) :: Cons b b a a => ASetter s t b b -> a -> s -> t
#

Modify the target(s) of a Lens', Iso, Setter or Traversal using (<|).

Example1 expression
(["world"], ["lens"]) & _1 <|~ "hello"(["hello","world"],["lens"])
value(<<|~) :: Cons b b a a => LensLike (Tuple2 b) s t b b -> a -> s -> (b, t)
#

(<|) a value onto the target of a Lens and return the result.

When you do not need the result of the operation, (<|~) is more flexible.

value(<<|=)
  1. :: (MonadState s m, Cons b b a a)
  2. => LensLike (Tuple2 b) s s b b
  3. -> a
  4. -> m b
#

(<|) a value onto the target of a Lens into your Monad's state and return the result.

When you do not need the result of the operation, (<|=) is more flexible.

value(<<<|~) :: Cons b b a a => LensLike' (Tuple2 b) s b -> a -> s -> (b, s)
#

(<|) a value onto the target of a Lens and return the old result.

When you do not need the result of the operation, (<|~) is more flexible.

value(<<<|=)
  1. :: (MonadState s m, Cons b b a a)
  2. => LensLike (Tuple2 b) s s b b
  3. -> a
  4. -> m b
#

(<|) a value onto the target of a Lens into your Monad's state and return the old result.

When you do not need the result of the operation, (<|=) is more flexible.

patternpattern (:<) :: Cons b b a a => a -> b -> b
#

Snoc

13 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

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

snoc an element onto a container.

This is an infix alias for snoc.

Example1 expression
Seq.fromList [] |> afromList [a]
Example1 expression
Seq.fromList [b, c] |> afromList [b,c,a]
Example1 expression
LazyT.pack "hello" |> '!'"hello!"
valuesnoc :: Snoc s s a a => s -> a -> s
#

snoc an element onto a container.

Example1 expression
snoc (Seq.fromList []) afromList [a]
Example1 expression
snoc (Seq.fromList [b, c]) afromList [b,c,a]
Example1 expression
snoc (LazyT.pack "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 (LazyT.pack "hello!")Just ("hello",'!')
Example1 expression
unsnoc (LazyT.pack "")Nothing
Example1 expression
unsnoc (Seq.fromList [b,c,a])Just (fromList [b,c],a)
Example1 expression
unsnoc (Seq.fromList [])Nothing
value_init :: Snoc s s a a => Traversal' s s
#

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

Example1 expression
[a,b,c,d]^?_initJust [a,b,c]
Example1 expression
[]^?_initNothing
Example1 expression
[a,b] & _init .~ [c,d,e][c,d,e,b]
Example1 expression
[] & _init .~ [a,b][]
Example1 expression
[a,b,c,d] & _init.traverse %~ f[f a,f b,f c,d]
Example1 expression
[1,2,3]^?_initJust [1,2]
Example1 expression
[1,2,3,4]^?!_init[1,2,3]
Example1 expression
"hello"^._init"hell"
Example1 expression
""^._init""
_init :: Traversal' [a] [a]
_init :: Traversal' (Seq a) (Seq a)
_init :: Traversal' (Vector a) (Vector a)
value_last :: Snoc s s a a => Traversal' s a
#

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

Example1 expression
[a,b,c]^?!_lastc
Example1 expression
[]^?_lastNothing
Example1 expression
[a,b,c] & _last %~ f[a,b,f c]
Example1 expression
[1,2]^?_lastJust 2
Example1 expression
[] & _last .~ 1[]
Example1 expression
[0] & _last .~ 2[2]
Example1 expression
[0,1] & _last .~ 2[0,2]

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

Example1 expression
Vector.fromList "abcde" ^? _lastJust 'e'
Example1 expression
Vector.empty ^? _lastNothing
Example1 expression
(Vector.fromList "abcde" & _last .~ 'Q') == Vector.fromList "abcdQ"True
_last :: Traversal' [a] a
_last :: Traversal' (Seq a) a
_last :: Traversal' (Vector a) a
value(|>~) :: Snoc b b a a => ASetter s t b b -> a -> s -> t
#

Modify the target(s) of a Lens', Iso, Setter or Traversal using (|>).

Example1 expression
(["world"], ["lens"]) & _1 |>~ "hello"(["world","hello"],["lens"])
value(<|>~) :: Snoc b b p p => LensLike (Tuple2 b) s t b b -> p -> s -> (b, t)
#

(|>) a value onto the target of a Lens and return the result.

When you do not need the result of the operation, (|>~) is more flexible.

value(<|>=)
  1. :: (MonadState s m, Snoc b b p p)
  2. => LensLike (Tuple2 b) s s b b
  3. -> p
  4. -> m b
#

(|>) a value onto the target of a Lens into your Monad's state and return the result.

When you do not need the result of the operation, (|>=) is more flexible.

value(<<|>~) :: Snoc b b p p => LensLike' (Tuple2 b) s b -> p -> s -> (b, s)
#

(|>) a value onto the target of a Lens and return the old result.

When you do not need the result of the operation, (|>~) is more flexible.

value(<<|>=)
  1. :: (MonadState s m, Snoc b b p p)
  2. => LensLike (Tuple2 b) s s b b
  3. -> p
  4. -> m b
#

(|>) a value onto the target of a Lens into your Monad's state and return the old result.

When you do not need the result of the operation, (|>=) is more flexible.

patternpattern (:>) :: Snoc a a b b => a -> b -> a
#