-- |
-- 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 :: Prism' (Maybe a) ()
_Nothing =
  (() -> Maybe a)
-> (Maybe a -> Either (Maybe a) ()) -> Prism' (Maybe a) ()
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
    (\ () -> Maybe a
forall a. Maybe a
Nothing)
    (\ Maybe a
x ->
      case Maybe a
x of
        Maybe a
Nothing -> () -> Either (Maybe a) ()
forall a b. b -> Either a b
Right ()
        Just a
y  -> Maybe a -> Either (Maybe a) ()
forall a b. a -> Either a b
Left (a -> Maybe a
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 :: Prism (Maybe a) (Maybe b) a b
_Just =
  (b -> Maybe b)
-> (Maybe a -> Either (Maybe b) a) -> Prism (Maybe a) (Maybe b) a b
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
    b -> Maybe b
forall a. a -> Maybe a
Just
    (\ Maybe a
x ->
      case Maybe a
x of
        Maybe a
Nothing -> Maybe b -> Either (Maybe b) a
forall a b. a -> Either a b
Left Maybe b
forall a. Maybe a
Nothing
        Just a
y  -> a -> Either (Maybe b) a
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 %? :: 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 Optic k is s t (Maybe u) (Maybe v)
-> Optic A_Prism NoIx (Maybe u) (Maybe v) u v
-> Optic k' is s t u v
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 A_Prism NoIx (Maybe u) (Maybe v) u v
forall a b. Prism (Maybe a) (Maybe b) a b
_Just Optic k' is s t u v -> Optic l js u v a b -> Optic m ks s t a b
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 (%?) #-}