module Utils where
import Control.Exception ( SomeException(SomeException), throwIO )
import Control.Monad ( Monad, return, (>>=), (>>), fail )
import Data.Bool ( Bool(..) )
import Data.Eq ( Eq )
import Data.Function ( flip, id )
import Data.Functor ( Functor, (<$>), (<$) )
import Data.Maybe ( Maybe(Nothing, Just), maybe )
import Prelude ( ($!) )
import System.IO ( IO )
import Data.Eq.Unicode ( (≡) )
import Data.Function.Unicode ( (∘) )
import Control.Concurrent.STM ( STM )
import Control.Concurrent.STM.TMVar ( TMVar, tryTakeTMVar, putTMVar )
import Control.Concurrent.STM.TVar ( TVar, readTVar, writeTVar )
isJustTrue ∷ Maybe Bool → Bool
isJustTrue = maybe False id
justEq ∷ Eq α ⇒ α → Maybe α → Bool
justEq = maybe False ∘ (≡)
(<$$>) ∷ Functor f ⇒ f α → (α → β) → f β
(<$$>) = flip (<$>)
void ∷ Functor f ⇒ f α → f ()
void = (() <$)
ifM ∷ Monad m ⇒ m Bool → m α → m α → m α
ifM c t e = c >>= \b → if b then t else e
throwInner ∷ SomeException → IO α
throwInner (SomeException e) = throwIO e
tryReadTMVar ∷ TMVar α → STM (Maybe α)
tryReadTMVar mv = do mx ← tryTakeTMVar mv
case mx of
Nothing → return mx
Just x → putTMVar mv x >> return mx
modifyTVar ∷ TVar α → (α → α) → STM ()
modifyTVar tv f = readTVar tv >>= writeTVar tv ∘! f
(∘!) ∷ (β → γ) → (α → β) → (α → γ)
f ∘! g = \x → f $! g x