module Validation.Combinators
( validateAll
, whenSuccess
, whenFailure
, whenSuccess_
, whenFailure_
, whenSuccessM
, whenFailureM
, whenSuccessM_
, whenFailureM_
, failureToMaybe
, successToMaybe
, maybeToFailure
, maybeToSuccess
) where
import Data.Foldable (foldl')
import {-# SOURCE #-} Validation (Validation (..), validation)
validateAll
:: forall e b a f
. (Foldable f, Semigroup e)
=> f (a -> Validation e b)
-> a
-> Validation e a
validateAll :: forall e b a (f :: * -> *).
(Foldable f, Semigroup e) =>
f (a -> Validation e b) -> a -> Validation e a
validateAll f (a -> Validation e b)
fs a
a = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Validation e a
res a -> Validation e b
f -> Validation e a
res forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* a -> Validation e b
f a
a) (forall e a. a -> Validation e a
Success a
a) f (a -> Validation e b)
fs
{-# INLINE validateAll #-}
whenFailure :: Applicative f => x -> Validation e a -> (e -> f x) -> f x
whenFailure :: forall (f :: * -> *) x e a.
Applicative f =>
x -> Validation e a -> (e -> f x) -> f x
whenFailure x
_ (Failure e
e) e -> f x
f = e -> f x
f e
e
whenFailure x
a (Success a
_) e -> f x
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure x
a
{-# INLINE whenFailure #-}
whenFailure_ :: Applicative f => Validation e a -> (e -> f ()) -> f ()
whenFailure_ :: forall (f :: * -> *) e a.
Applicative f =>
Validation e a -> (e -> f ()) -> f ()
whenFailure_ = forall (f :: * -> *) x e a.
Applicative f =>
x -> Validation e a -> (e -> f x) -> f x
whenFailure ()
{-# INLINE whenFailure_ #-}
whenFailureM :: Monad m => x -> m (Validation e a) -> (e -> m x) -> m x
whenFailureM :: forall (m :: * -> *) x e a.
Monad m =>
x -> m (Validation e a) -> (e -> m x) -> m x
whenFailureM x
x m (Validation e a)
mv e -> m x
f = m (Validation e a)
mv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Validation e a
v -> forall (f :: * -> *) x e a.
Applicative f =>
x -> Validation e a -> (e -> f x) -> f x
whenFailure x
x Validation e a
v e -> m x
f
{-# INLINE whenFailureM #-}
whenFailureM_ :: Monad m => m (Validation e a) -> (e -> m ()) -> m ()
whenFailureM_ :: forall (m :: * -> *) e a.
Monad m =>
m (Validation e a) -> (e -> m ()) -> m ()
whenFailureM_ m (Validation e a)
mv e -> m ()
f = m (Validation e a)
mv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Validation e a
v -> forall (f :: * -> *) e a.
Applicative f =>
Validation e a -> (e -> f ()) -> f ()
whenFailure_ Validation e a
v e -> m ()
f
{-# INLINE whenFailureM_ #-}
whenSuccess :: Applicative f => x -> Validation e a -> (a -> f x) -> f x
whenSuccess :: forall (f :: * -> *) x e a.
Applicative f =>
x -> Validation e a -> (a -> f x) -> f x
whenSuccess x
x (Failure e
_) a -> f x
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure x
x
whenSuccess x
_ (Success a
a) a -> f x
f = a -> f x
f a
a
{-# INLINE whenSuccess #-}
whenSuccess_ :: Applicative f => Validation e a -> (a -> f ()) -> f ()
whenSuccess_ :: forall (f :: * -> *) e a.
Applicative f =>
Validation e a -> (a -> f ()) -> f ()
whenSuccess_ = forall (f :: * -> *) x e a.
Applicative f =>
x -> Validation e a -> (a -> f x) -> f x
whenSuccess ()
{-# INLINE whenSuccess_ #-}
whenSuccessM :: Monad m => x -> m (Validation e a) -> (a -> m x) -> m x
whenSuccessM :: forall (m :: * -> *) x e a.
Monad m =>
x -> m (Validation e a) -> (a -> m x) -> m x
whenSuccessM x
x m (Validation e a)
mv a -> m x
f = m (Validation e a)
mv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Validation e a
v -> forall (f :: * -> *) x e a.
Applicative f =>
x -> Validation e a -> (a -> f x) -> f x
whenSuccess x
x Validation e a
v a -> m x
f
{-# INLINE whenSuccessM #-}
whenSuccessM_ :: Monad m => m (Validation e a) -> (a -> m ()) -> m ()
whenSuccessM_ :: forall (m :: * -> *) e a.
Monad m =>
m (Validation e a) -> (a -> m ()) -> m ()
whenSuccessM_ m (Validation e a)
mv a -> m ()
f = m (Validation e a)
mv forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Validation e a
v -> forall (f :: * -> *) e a.
Applicative f =>
Validation e a -> (a -> f ()) -> f ()
whenSuccess_ Validation e a
v a -> m ()
f
{-# INLINE whenSuccessM_ #-}
failureToMaybe :: Validation e a -> Maybe e
failureToMaybe :: forall e a. Validation e a -> Maybe e
failureToMaybe = forall e x a. (e -> x) -> (a -> x) -> Validation e a -> x
validation forall a. a -> Maybe a
Just (forall a b. a -> b -> a
const forall a. Maybe a
Nothing)
{-# INLINE failureToMaybe #-}
successToMaybe :: Validation e a -> Maybe a
successToMaybe :: forall e a. Validation e a -> Maybe a
successToMaybe = forall e x a. (e -> x) -> (a -> x) -> Validation e a -> x
validation (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just
{-# INLINE successToMaybe #-}
maybeToFailure :: a -> Maybe e -> Validation e a
maybeToFailure :: forall a e. a -> Maybe e -> Validation e a
maybeToFailure a
a = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall e a. a -> Validation e a
Success a
a) forall e a. e -> Validation e a
Failure
{-# INLINE maybeToFailure #-}
maybeToSuccess :: e -> Maybe a -> Validation e a
maybeToSuccess :: forall e a. e -> Maybe a -> Validation e a
maybeToSuccess e
e = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall e a. e -> Validation e a
Failure e
e) forall e a. a -> Validation e a
Success
{-# INLINE maybeToSuccess #-}