{-# OPTIONS_GHC -Wno-orphans #-}
module Calamity.Internal.Utils
( whenJust
, whenM
, unlessM
, lastMaybe
, leftToMaybe
, rightToMaybe
, justToEither
, (<<$>>)
, (<<*>>)
, (<.>)
, debug
, info
, Calamity.Internal.Utils.error
, swap ) where
import Calamity.Types.LogEff
import Control.Applicative
import Data.Default.Class
import qualified Data.HashMap.Lazy as LH
import qualified Data.Map as M
import Data.Semigroup ( Last(..) )
import Data.Text.Lazy
import Data.Time
import qualified Data.Vector.Unboxed as VU
import Data.Vector.Unboxed ( Vector )
import qualified DiPolysemy as Di
import qualified Polysemy as P
import TextShow
whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
whenJust :: Maybe a -> (a -> m ()) -> m ()
whenJust = ((a -> m ()) -> Maybe a -> m ()) -> Maybe a -> (a -> m ()) -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((a -> m ()) -> Maybe a -> m ())
-> Maybe a -> (a -> m ()) -> m ())
-> ((a -> m ()) -> Maybe a -> m ())
-> Maybe a
-> (a -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ m () -> (a -> m ()) -> Maybe a -> m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
whenM :: Monad m => m Bool -> m () -> m ()
whenM :: m Bool -> m () -> m ()
whenM p :: m Bool
p m :: m ()
m = m Bool
p m Bool -> (Bool -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
True -> m ()
m
_ -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: m Bool -> m () -> m ()
unlessM = m Bool -> m () -> m ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM (m Bool -> m () -> m ())
-> (m Bool -> m Bool) -> m Bool -> m () -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Bool
not (Bool -> Bool) -> m Bool -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)
lastMaybe :: Maybe a -> Maybe a -> Maybe a
lastMaybe :: Maybe a -> Maybe a -> Maybe a
lastMaybe l :: Maybe a
l r :: Maybe a
r = Last a -> a
forall a. Last a -> a
getLast (Last a -> a) -> Maybe (Last a) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> Last a) -> Maybe a -> Maybe (Last a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Last a
forall a. a -> Last a
Last Maybe a
l Maybe (Last a) -> Maybe (Last a) -> Maybe (Last a)
forall a. Semigroup a => a -> a -> a
<> (a -> Last a) -> Maybe a -> Maybe (Last a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Last a
forall a. a -> Last a
Last Maybe a
r
leftToMaybe :: Either e a -> Maybe e
leftToMaybe :: Either e a -> Maybe e
leftToMaybe (Left x :: e
x) = e -> Maybe e
forall a. a -> Maybe a
Just e
x
leftToMaybe _ = Maybe e
forall a. Maybe a
Nothing
rightToMaybe :: Either e a -> Maybe a
rightToMaybe :: Either e a -> Maybe a
rightToMaybe (Right x :: a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
rightToMaybe _ = Maybe a
forall a. Maybe a
Nothing
justToEither :: Maybe e -> Either e ()
justToEither :: Maybe e -> Either e ()
justToEither (Just x :: e
x) = e -> Either e ()
forall a b. a -> Either a b
Left e
x
justToEither _ = () -> Either e ()
forall a b. b -> Either a b
Right ()
(<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
<<$>> :: (a -> b) -> f (g a) -> f (g b)
(<<$>>) = (g a -> g b) -> f (g a) -> f (g b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((g a -> g b) -> f (g a) -> f (g b))
-> ((a -> b) -> g a -> g b) -> (a -> b) -> f (g a) -> f (g b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
infixl 4 <<$>>
(<<*>>) :: (Applicative f, Applicative g) => f (g (a -> b)) -> f (g a) -> f (g b)
<<*>> :: f (g (a -> b)) -> f (g a) -> f (g b)
(<<*>>) = (g (a -> b) -> g a -> g b) -> f (g (a -> b)) -> f (g a) -> f (g b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 g (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)
infixl 4 <<*>>
(<.>) :: Functor f => (a -> b) -> (c -> f a) -> (c -> f b)
<.> :: (a -> b) -> (c -> f a) -> c -> f b
(<.>) f :: a -> b
f g :: c -> f a
g x :: c
x = a -> b
f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> c -> f a
g c
x
infixl 4 <.>
debug :: P.Member LogEff r => Text -> P.Sem r ()
debug :: Text -> Sem r ()
debug = Text -> Sem r ()
forall msg path (r :: [(* -> *) -> * -> *]).
(ToMessage msg, Member (Di Level path Message) r) =>
msg -> Sem r ()
Di.debug
info :: P.Member LogEff r => Text -> P.Sem r ()
info :: Text -> Sem r ()
info = Text -> Sem r ()
forall msg path (r :: [(* -> *) -> * -> *]).
(ToMessage msg, Member (Di Level path Message) r) =>
msg -> Sem r ()
Di.info
error :: P.Member LogEff r => Text -> P.Sem r ()
error :: Text -> Sem r ()
error = Text -> Sem r ()
forall msg path (r :: [(* -> *) -> * -> *]).
(ToMessage msg, Member (Di Level path Message) r) =>
msg -> Sem r ()
Di.error
swap :: (a, b) -> (b, a)
swap :: (a, b) -> (b, a)
swap ~(a :: a
a, b :: b
b) = (b
b, a
a)
instance TextShow UTCTime where
showb :: UTCTime -> Builder
showb = String -> Builder
fromString (String -> Builder) -> (UTCTime -> String) -> UTCTime -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UTCTime -> String
forall a. Show a => a -> String
show
instance (TextShow a, VU.Unbox a) => TextShow (Vector a) where
showb :: Vector a -> Builder
showb = [a] -> Builder
forall a. TextShow a => [a] -> Builder
showbList ([a] -> Builder) -> (Vector a -> [a]) -> Vector a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> [a]
forall a. Unbox a => Vector a -> [a]
VU.toList
instance (Show k, Show v) => TextShow (LH.HashMap k v) where
showb :: HashMap k v -> Builder
showb = String -> Builder
fromString (String -> Builder)
-> (HashMap k v -> String) -> HashMap k v -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap k v -> String
forall a. Show a => a -> String
show
instance (Show k, Show v) => TextShow (M.Map k v) where
showb :: Map k v -> Builder
showb = String -> Builder
fromString (String -> Builder) -> (Map k v -> String) -> Map k v -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k v -> String
forall a. Show a => a -> String
show
instance Default (M.Map k v) where
def :: Map k v
def = Map k v
forall k v. Map k v
M.empty