Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Last active August 20, 2018 18:23
Show Gist options
  • Save joshuakfarrar/d705c5f462b21cf20c906afc5eb8dcf8 to your computer and use it in GitHub Desktop.
Save joshuakfarrar/d705c5f462b21cf20c906afc5eb8dcf8 to your computer and use it in GitHub Desktop.
Using ExceptT to catch errors in Haskell.
{-# LANGUAGE NoImplicitPrelude #-}
module Main where
import Protolude
import qualified Data.Text as T
calculateThing :: ExceptT Text IO Text
calculateThing = do
print $ T.pack "log"
return $ T.pack "uccess"
--throwError $ T.pack "fail"
main :: IO ()
main = do
{-
need to modify the wrapped value?
fmap a lambda over ExceptT! (fmap (b -> c) (m a b) returns (m a c))
-}
result <- runExceptT $ fmap (\x -> T.cons 's' x) calculateThing
case result of
Right success -> putStrLn success
Left error -> putStrLn error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment