0 | module Data.Maybe.Lens
 1 |
 2 | import Data.Maybe
 3 | import Data.Profunctor
 4 | import public Control.Lens
 5 |
 6 | %default total
 7 |
 8 |
 9 | ||| A prism of the `Nothing` case of a `Maybe`.
10 | public export
11 | Nothing_ : Prism (Maybe a) (Maybe b) () ()
12 | Nothing_ = prism (const Nothing) (maybe (Right ()) (const $ Left Nothing))
13 |
14 | ||| A prism of the `Just` case of a `Maybe`.
15 | public export
16 | Just_ : Prism (Maybe a) (Maybe b) a b
17 | Just_ = prism Just $ \case
18 |   Nothing => Left Nothing
19 |   Just x => Right x
20 |
21 | export infixl 9 .?
22 |
23 | ||| The composition `l .? l'` is equivalent to `l . Just_ . l'`.
24 | ||| Useful for optics whose focus type is a `Maybe`, such as `at`.
25 | public export
26 | (.?) : IsPrism p => Optic' p s t (Maybe a) (Maybe b) -> Optic' p a b a' b' -> Optic' p s t a' b'
27 | l .? l' = l . Just_ . l'
28 |
29 |
30 | public export
31 | Each (Maybe a) (Maybe b) a b where
32 |   -- each = Just_
33 |   each = traversed
34 |