| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Optic.Prism.Snoc
Contents
Synopsis
- 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
- pattern (:>) :: forall a b. Snoc a a b b => a -> b -> a
Snoc
class 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.
Minimal complete definition
Methods
Instances
| Snoc ByteString ByteString Word8 Word8 | |
Defined in Control.Lens.Cons Methods _Snoc :: Prism ByteString ByteString (ByteString, Word8) (ByteString, Word8) # | |
| Snoc ByteString ByteString Word8 Word8 | |
Defined in Control.Lens.Cons Methods _Snoc :: Prism ByteString ByteString (ByteString, Word8) (ByteString, Word8) # | |
| Snoc Text Text Char Char | |
| Snoc Text Text Char Char | |
| Snoc [a] [b] a b | |
Defined in Control.Lens.Cons | |
| Snoc (ZipList a) (ZipList b) a b | |
| Snoc (Seq a) (Seq 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 (Vector a) (Vector b) a b | |
snoc :: Snoc s s a a => s -> a -> s infixl 5 #
snoc an element onto the end of a container.
>>>snoc (Seq.fromList []) afromList [a]
>>>snoc (Seq.fromList [b, c]) afromList [b,c,a]
>>>snoc (LazyT.pack "hello") '!'"hello!"
unsnoc :: 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.
>>>unsnoc (LazyT.pack "hello!")Just ("hello",'!')
>>>unsnoc (LazyT.pack "")Nothing
>>>unsnoc (Seq.fromList [b,c,a])Just (fromList [b,c],a)
>>>unsnoc (Seq.fromList [])Nothing
_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.
>>>[a,b,c,d]^?_initJust [a,b,c]
>>>[]^?_initNothing
>>>[a,b] & _init .~ [c,d,e][c,d,e,b]
>>>[] & _init .~ [a,b][]
>>>[a,b,c,d] & _init.traverse %~ f[f a,f b,f c,d]
>>>[1,2,3]^?_initJust [1,2]
>>>[1,2,3,4]^?!_init[1,2,3]
>>>"hello"^._init"hell"
>>>""^._init""
_init::Traversal'[a] [a]_init::Traversal'(Seqa) (Seqa)_init::Traversal'(Vectora) (Vectora)
_last :: Snoc s s a a => Traversal' s a #
A Traversal reading and writing to the last element of a non-empty container.
>>>[a,b,c]^?!_lastc
>>>[]^?_lastNothing
>>>[a,b,c] & _last %~ f[a,b,f c]
>>>[1,2]^?_lastJust 2
>>>[] & _last .~ 1[]
>>>[0] & _last .~ 2[2]
>>>[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.
>>>Vector.fromList "abcde" ^? _lastJust 'e'
>>>Vector.empty ^? _lastNothing
>>>(Vector.fromList "abcde" & _last .~ 'Q') == Vector.fromList "abcdQ"True
_last::Traversal'[a] a_last::Traversal'(Seqa) a_last::Traversal'(Vectora) a