-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
multipleExitPoints
helper to H.S.Util.Exit module
- Loading branch information
Showing
3 changed files
with
42 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Hackage.Security.Util.Exit where | ||
|
||
import Control.Monad.Except | ||
|
||
{------------------------------------------------------------------------------- | ||
Auxiliary: multiple exit points | ||
-------------------------------------------------------------------------------} | ||
|
||
-- | Multiple exit points | ||
-- | ||
-- We can simulate the imperative code | ||
-- | ||
-- > if (cond1) | ||
-- > return exp1; | ||
-- > if (cond2) | ||
-- > return exp2; | ||
-- > if (cond3) | ||
-- > return exp3; | ||
-- > return exp4; | ||
-- | ||
-- as | ||
-- | ||
-- > multipleExitPoints $ do | ||
-- > when (cond1) $ | ||
-- > exit exp1 | ||
-- > when (cond) $ | ||
-- > exit exp2 | ||
-- > when (cond) | ||
-- > exit exp3 | ||
-- > return exp4 | ||
multipleExitPoints :: Monad m => ExceptT a m a -> m a | ||
multipleExitPoints = liftM aux . runExceptT | ||
where | ||
aux :: Either a a -> a | ||
aux (Left a) = a | ||
aux (Right a) = a | ||
|
||
-- | Function exit point (see 'multipleExitPoints') | ||
exit :: Monad m => e -> ExceptT e m a | ||
exit = throwError |