-- |
-- 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.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 #-}