module Data.Maybe.Justify
       ( justify
       ) where

import Data.Maybe

-- | Control flow has always been obtuse, and justify makes simple
-- if-then-else statements simple one liners.
--
-- === Examples
--
-- Realistically this is very ineffecient, but interesting to see
--
-- >>> justify fromJust isJust (Maybe True) == id (Maybe True)
-- True
--
-- A somewhat more realistic example where you generate a maybe with
-- control flow then make a change to the returned value with a
-- provided default.
-- 
-- >>> maybe "Default" (<> "!") (justify (<> " World") (/= mempty) "Hello")
-- "Hello World!"
justify :: (a -> b) -> (a -> Bool) -> a -> Maybe b
justify _if justif y = if justif y then (Just . _if) y else Nothing