Safe Haskell | None |
---|---|
Language | Haskell2010 |
A neat effect that you can use to get early returns in your functions. Here's how to use it.
Before:
f = do m1 <- maybeFunc1 case m1 of Nothing -> return "1 nothing" Just x -> do m2 <- maybeFunc2 case m2 of Nothing -> return "2 nothing" Just y -> return (x <> y)
After:
f = handleEarly $ do m1 <- maybeFunc1 x <- ifNothingEarlyReturn "1 nothing" m1 m2 <- maybeFunc2 y <- ifNothingEarlyReturn "2 nothing" m2 return (x <> y)
You can use the earlyReturn
function directly, or one of the helpers for common use cases.
Synopsis
- module Control.Effects
- newtype Early a m = EarlyMethods {
- _earlyReturn :: forall b. a -> m b
- earlyReturn :: forall a b m. MonadEffect (Early a) m => a -> m b
- handleEarly :: Monad m => ExceptT (EarlyValue a) m a -> m a
- onlyDo :: MonadEffect (Early a) m => m a -> m b
- ifNothingEarlyReturn :: MonadEffect (Early a) m => a -> Maybe b -> m b
- ifNothingDo :: MonadEffect (Early a) m => m a -> Maybe b -> m b
- ifLeftEarlyReturn :: MonadEffect (Early c) m => (a -> c) -> Either a b -> m b
- ifLeftDo :: MonadEffect (Early c) m => (a -> m c) -> Either a b -> m b
Documentation
module Control.Effects
EarlyMethods | |
|
Instances
Effect (Early a) Source # | |
Defined in Control.Effects.Early type CanLift (Early a) t :: Constraint Source # type ExtraConstraint (Early a) m :: Constraint Source # | |
type CanLift (Early a) t Source # | |
Defined in Control.Effects.Early | |
type ExtraConstraint (Early a) m Source # | |
Defined in Control.Effects.Early |
earlyReturn :: forall a b m. MonadEffect (Early a) m => a -> m b Source #
Allows you to return early from a function. Make sure you handleEarly
to get the actual
result out.
handleEarly :: Monad m => ExceptT (EarlyValue a) m a -> m a Source #
Get the result from a computation. Either the early returned one, or the regular result.
onlyDo :: MonadEffect (Early a) m => m a -> m b Source #
Only do the given action and exit early with it's result.
ifNothingEarlyReturn :: MonadEffect (Early a) m => a -> Maybe b -> m b Source #
ifNothingDo :: MonadEffect (Early a) m => m a -> Maybe b -> m b Source #
ifLeftEarlyReturn :: MonadEffect (Early c) m => (a -> c) -> Either a b -> m b Source #