This class provides a way to attach or detach elements on the left side of a structure in a flexible manner.
Moduleoptics-core-0.4.1.1Haskell2010
Optics.Cons.Core
This module defines the Cons and Snoc classes, which provide Prisms 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.
- 2 classes
- 10 values
- Packageoptics-core-0.4.1.1
- Exports14
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceCore.hs
Cons
7 declarationscons an element onto a container.
cons 'a' """a"
cons 'a' "bc""abc"
Attempt to extract the left-most element from a container, and a version of the container without that element.
uncons []Nothing
uncons [1, 2, 3]Just (1,[2,3])
An AffineTraversal reading and writing to the head of a non-empty container.
"abc" ^? _headJust 'a'
"abc" & _head .~ 'd'"dbc"
[1,2,3] & _head %~ (*10)[10,2,3]
[] & _head %~ absurd[]
[1,2,3] ^? _headJust 1
[] ^? _headNothing
[1,2] ^? _headJust 1
[] & _head .~ 1[]
[0] & _head .~ 2[2]
[0,1] & _head .~ 2[2,1]
An AffineTraversal reading and writing to the tail of a non-empty container.
"ab" & _tail .~ "cde""acde"
[] & _tail .~ [1,2][]
[1,2,3,4,5] & _tail % traversed %~ (*10)[1,20,30,40,50]
[1,2] & _tail .~ [3,4,5][1,3,4,5]
[] & _tail .~ [1,2][]
"abc" ^? _tailJust "bc"
"hello" ^? _tailJust "ello"
"" ^? _tailNothing
Pattern synonym for matching on the leftmost element of a structure.
case ['a','b','c'] of (x :< _) -> x'a'
Snoc
7 declarationsThis class provides a way to attach or detach elements on the right side of a structure in a flexible manner.
snoc an element onto the end of a container.
snoc "hello" '!'"hello!"
Attempt to extract the right-most element from a container, and a version of the container without that element.
unsnoc "hello!"Just ("hello",'!')
unsnoc ""Nothing
An AffineTraversal reading and replacing all but the a last element of a non-empty container.
"abcd" ^? _initJust "abc"
"" ^? _initNothing
"ab" & _init .~ "cde""cdeb"
[] & _init .~ [1,2][]
[1,2,3,4] & _init % traversed %~ (*10)[10,20,30,4]
[1,2,3] ^? _initJust [1,2]
"hello" ^? _initJust "hell"
[] ^? _initNothing
An AffineTraversal reading and writing to the last element of a non-empty container.
"abc" ^? _lastJust 'c'
"" ^? _lastNothing
[1,2,3] & _last %~ (+1)[1,2,4]
[1,2] ^? _lastJust 2
[] & _last .~ 1[]
[0] & _last .~ 2[2]
[0,1] & _last .~ 2[0,2]
Pattern synonym for matching on the rightmost element of a structure.
case ['a','b','c'] of (_ :> x) -> x'c'