{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module PlutusTx.Maybe (Maybe(..), isJust, isNothing, maybe, fromMaybe, mapMaybe) where
import PlutusTx.Base (id)
import PlutusTx.Bool
import PlutusTx.List (foldr)
import Prelude (Maybe (..))
{-# INLINABLE isJust #-}
isJust :: Maybe a -> Bool
isJust :: Maybe a -> Bool
isJust Maybe a
m = case Maybe a
m of { Just a
_ -> Bool
True; Maybe a
_ -> Bool
False; }
{-# INLINABLE isNothing #-}
isNothing :: Maybe a -> Bool
isNothing :: Maybe a -> Bool
isNothing Maybe a
m = case Maybe a
m of { Just a
_ -> Bool
False; Maybe a
_ -> Bool
True; }
{-# INLINABLE maybe #-}
maybe :: b -> (a -> b) -> Maybe a -> b
maybe :: b -> (a -> b) -> Maybe a -> b
maybe b
b a -> b
f Maybe a
m = case Maybe a
m of
Maybe a
Nothing -> b
b
Just a
a -> a -> b
f a
a
{-# INLINABLE fromMaybe #-}
fromMaybe :: a -> Maybe a -> a
fromMaybe :: a -> Maybe a -> a
fromMaybe a
a = a -> (a -> a) -> Maybe a -> a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe a
a a -> a
forall a. a -> a
id
{-# INLINABLE mapMaybe #-}
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
mapMaybe a -> Maybe b
p = (a -> [b] -> [b]) -> [b] -> [a] -> [b]
forall a b. (a -> b -> b) -> b -> [a] -> b
foldr (\a
e [b]
xs -> [b] -> (b -> [b]) -> Maybe b -> [b]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [b]
xs (b -> [b] -> [b]
forall a. a -> [a] -> [a]
:[b]
xs) (a -> Maybe b
p a
e)) []