-- |
-- Module: Data.Maybe.Optics
-- Description: 'Prism's for the 'Maybe' datatype.
--
-- This module defines 'Prism's for the constructors of the 'Maybe' datatype.
module Data.Maybe.Optics
  ( _Nothing
  , _Just
  , (%?)
  )
  where

import Optics.Internal.Optic
import Optics.Prism

-- | A 'Prism' that matches on the 'Nothing' constructor of 'Maybe'.
_Nothing :: Prism' (Maybe a) ()
_Nothing :: forall a. Prism' (Maybe a) ()
_Nothing =
  forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
    (\ () -> forall a. Maybe a
Nothing)
    (\ Maybe a
x ->
      case Maybe a
x of
        Maybe a
Nothing -> forall a b. b -> Either a b
Right ()
        Just a
y  -> forall a b. a -> Either a b
Left (forall a. a -> Maybe a
Just a
y)
    )
{-# INLINE _Nothing #-}

-- | A 'Prism' that matches on the 'Just' constructor of 'Maybe'.
_Just :: Prism (Maybe a) (Maybe b) a b
_Just :: forall a b. Prism (Maybe a) (Maybe b) a b
_Just =
  forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
    forall a. a -> Maybe a
Just
    (\ Maybe a
x ->
      case Maybe a
x of
        Maybe a
Nothing -> forall a b. a -> Either a b
Left forall a. Maybe a
Nothing
        Just a
y  -> forall a b. b -> Either a b
Right a
y
    )
{-# INLINE _Just #-}

-- | Shortcut for @'%' '_Just' '%'@.
--
-- Useful for composing lenses of 'Maybe' type.
--
-- @since 0.4.1
infixl 9 %?
(%?)
  :: (AppendIndices is js ks, JoinKinds k A_Prism k', JoinKinds k' l m)
  => Optic k is s t (Maybe u) (Maybe v)
  -> Optic l js u v a b
  -> Optic m ks s t a b
Optic k is s t (Maybe u) (Maybe v)
o1 %? :: forall (is :: IxList) (js :: IxList) (ks :: IxList) k k' l m s t u
       v a b.
(AppendIndices is js ks, JoinKinds k A_Prism k',
 JoinKinds k' l m) =>
Optic k is s t (Maybe u) (Maybe v)
-> Optic l js u v a b -> Optic m ks s t a b
%? Optic l js u v a b
o2 = Optic k is s t (Maybe u) (Maybe v)
o1 forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall a b. Prism (Maybe a) (Maybe b) a b
_Just forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
       b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic l js u v a b
o2
{-# INLINE (%?) #-}