module Ki.Prelude
  ( atomicallyIO,
    onLeft,
    whenJust,
    whenLeft,
    whenM,
    module X,
  )
where

import Control.Applicative as X (optional, (<|>))
import Control.Exception as X (Exception, SomeException)
import Control.Monad as X (join, unless)
import Data.Coerce as X (coerce)
import Data.Foldable as X (for_)
import Data.Function as X (fix)
import Data.Functor as X (void, ($>), (<&>))
import Data.IntMap.Strict as X (IntMap)
import Data.Map.Strict as X (Map)
import Data.Maybe as X (fromMaybe)
import Data.Set as X (Set)
import Data.Word as X (Word32)
import GHC.Generics as X (Generic)
import Ki.Concurrency as X
import Prelude as X hiding (IO)

atomicallyIO :: STM (IO a) -> IO a
atomicallyIO :: STM (IO a) -> IO a
atomicallyIO =
  IO (IO a) -> IO a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (IO (IO a) -> IO a)
-> (STM (IO a) -> IO (IO a)) -> STM (IO a) -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM (IO a) -> IO (IO a)
forall a. STM a -> IO a
atomically

onLeft :: (a -> IO b) -> Either a b -> IO b
onLeft :: (a -> IO b) -> Either a b -> IO b
onLeft a -> IO b
f =
  (a -> IO b) -> (b -> IO b) -> Either a b -> IO b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> IO b
f b -> IO b
forall (f :: * -> *) a. Applicative f => a -> f a
pure

whenJust :: Maybe a -> (a -> IO ()) -> IO ()
whenJust :: Maybe a -> (a -> IO ()) -> IO ()
whenJust Maybe a
x a -> IO ()
f =
  IO () -> (a -> IO ()) -> Maybe a -> IO ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) a -> IO ()
f Maybe a
x

whenLeft :: Either a b -> (a -> IO b) -> IO b
whenLeft :: Either a b -> (a -> IO b) -> IO b
whenLeft Either a b
x a -> IO b
f =
  (a -> IO b) -> (b -> IO b) -> Either a b -> IO b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> IO b
f b -> IO b
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either a b
x

whenM :: IO Bool -> IO () -> IO ()
whenM :: IO Bool -> IO () -> IO ()
whenM IO Bool
x IO ()
y =
  IO Bool
x IO Bool -> (Bool -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Bool
False -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    Bool
True -> IO ()
y