Last active
September 5, 2018 12:51
Revisions
-
err0r500 revised this gist
Sep 5, 2018 . 1 changed file with 11 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,31 +5,29 @@ module Main where import Control.Monad.Identity import Control.Monad.Trans.Either import Control.Monad.Trans.Except data User = User { name :: String , firstName :: String } deriving (Show) class Monad m => UserGetter m where getName :: Int -> m (Either String String) getFirstname :: Int -> m (Either String String) class Monad m => BusinessLogic m where getCompleteUser :: Int -> m (Either String User) instance (UserGetter m, Monad m) => BusinessLogic m where getCompleteUser id = runExceptT $ do name <- ExceptT $ getName id firstName <- ExceptT $ getFirstname id return (User name firstName) instance UserGetter IO where getName _ = do -
err0r500 revised this gist
Sep 5, 2018 . 1 changed file with 20 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -27,4 +27,23 @@ instance (UserGetter m, Monad m) => BusinessLogic m where fn <- getFirstname id case fn of Left err -> return (Left err) Right firstName -> return $ Right (User name firstName) instance UserGetter IO where getName _ = do n <- getLine return (Right n) getFirstname _ = return (Right "ioUserFirstname") instance (Monad m) => UserGetter (IdentityT m) where getName _ = return (Right "identityUserName") getFirstname _ = return (Right "identityUserFirstname") main :: IO () main = do user1 <- runIdentityT (getCompleteUser 12) user2 <- getCompleteUser 13 print user1 print user2 -
err0r500 created this gist
Sep 5, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} module Main where import Control.Monad.Identity import Control.Monad.Trans.Either data User = User { name :: String , firstName :: String } deriving (Show) class Monad m => UserGetter m where getName :: Int -> m (Either String String) getFirstname :: Int -> m (Either String String) class Monad m => BusinessLogic m where getCompleteUser :: Int -> m (Either String User) instance (UserGetter m, Monad m) => BusinessLogic m where getCompleteUser id =do n <- getName id case n of Left err -> return (Left err) Right name -> do fn <- getFirstname id case fn of Left err -> return (Left err) Right firstName -> return $ Right (User name firstName)