Created
July 19, 2018 15:20
-
-
Save kendricktan/4702bf90a13a2212e8bbb9574ee49160 to your computer and use it in GitHub Desktop.
ExceptT minimal usage example
This file contains hidden or 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
| import Control.Monad.Except | |
| import Control.Monad.Identity | |
| data MyError = InvalidNumber deriving Show | |
| data MySuccess = CorrectNumber | NotACorrectNumber deriving Show | |
| runMyMonad = runIdentity . runExceptT | |
| type MyMonad = ExceptT MyError Identity MySuccess | |
| m :: Int -> MyMonad | |
| m x = do | |
| when (x /= 42) (throwError InvalidNumber) | |
| return CorrectNumber | |
| m2 :: Int -> MyMonad | |
| m2 x = do | |
| (m x) `catchError` (const $ return NotACorrectNumber) | |
| run1 = runMyMonad (m 0 ) | |
| run2 = runMyMonad (m2 0 ) | |
| run3 = runMyMonad (m 42) | |
| run4 = runMyMonad (m2 42) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment