packages feed

papa-0.7.0: src/Papa/Base/Data/Maybe.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Optional values. @fromJust@ is not exported: it is partial.
module Papa.Base.Data.Maybe (
  module P,
  whenJust,
  whenJustM,
) where

import Control.Monad (Monad (return, (>>=)))
import Data.Maybe as P (
  Maybe (Just, Nothing),
  catMaybes,
  fromMaybe,
  isJust,
  isNothing,
  listToMaybe,
  mapMaybe,
  maybe,
  maybeToList,
 )

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (Fn, fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linear (-100) 100)

-- | Apply a step to a value where it applies, and keep the value where it
-- does not: @p a@ is 'Just' a result, which @f@ turns back into a value, or
-- 'Nothing', which keeps @a@.
--
-- ==== __Examples__
--
-- >>> whenJust (\n -> if even n then Just (show n) else Nothing) length (10 :: Int)
-- 2
--
-- >>> whenJust (\n -> if even n then Just (show n) else Nothing) length (7 :: Int)
-- 7
--
-- >>> whenJust (preview _Left) (Right . length) (Left "abc" :: Either String Int)
-- Right 3
--
-- With 'Control.Category.id', a rewrite step that keeps the value where it does not apply:
--
-- >>> whenJust (\s -> stripPrefix "un" s) id "undo"
-- "do"
--
-- Where the step never applies, 'whenJust' is the identity:
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genInt :: Gen (Fn Int Int)); a <- forAll genInt; whenJust (const Nothing) (apply f) a === a
-- ...passed 100 tests.
-- True
--
-- Where the step always applies, it is the composition:
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genInt); g <- forAll (fn genInt); a <- forAll genInt; whenJust (Just . apply g) (apply f) a === apply f (apply g a)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
whenJust :: (a -> Maybe b) -> (b -> a) -> a -> a
whenJust p f a =
  maybe a f (p a)
{-# INLINE whenJust #-}

-- | 'whenJust' in a 'Monad': run the step on a value, and where its result
-- is 'Just', run @f@ on it; where it is 'Nothing', return the value.
--
-- ==== __Examples__
--
-- >>> whenJustM (\n -> [Just n, Nothing]) (\n -> [n + 1, n + 2]) (10 :: Int)
-- [11,12,10]
--
-- >>> whenJustM (\_ -> Just Nothing) (\_ -> Nothing) 'a'
-- Just 'a'
--
-- At 'Data.Functor.Identity.Identity', 'whenJustM' is 'whenJust':
--
-- >>> check $ withTests 100 $ property $ do p <- forAll (fn (Gen.maybe genInt)); f <- forAll (fn genInt); a <- forAll genInt; runIdentity (whenJustM (Identity . apply p) (Identity . apply f) a) === whenJust (apply p) (apply f) a
-- ...passed 100 tests.
-- True
--
-- Where the step never applies, it returns the value:
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn (Gen.maybe genInt) :: Gen (Fn Int (Maybe Int))); a <- forAll genInt; whenJustM (\_ -> Just Nothing) (apply f) a === Just a
-- ...passed 100 tests.
-- True
--
-- Where the step always applies, it is Kleisli composition:
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn (Gen.maybe genInt)); g <- forAll (fn (Gen.maybe genInt)); a <- forAll genInt; whenJustM (fmap Just . apply g) (apply f) a === (apply g >=> apply f) a
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
whenJustM :: (Monad f) => (a -> f (Maybe b)) -> (b -> f a) -> a -> f a
whenJustM p f a =
  p a >>= maybe (return a) f
{-# INLINE whenJustM #-}