Created
January 4, 2018 16:55
-
-
Save essic/a0931c71f6a12d7cc7c41cd478802188 to your computer and use it in GitHub Desktop.
Haskell playground created by essic - https://repl.it/@essic/Haskell-playground
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
{-# LANGUAGE NoImplicitPrelude #-} | |
import Data.List (find) | |
import Data.Monoid ((<>)) | |
import Data.Either (Either(..)) | |
import Data.Int (Int) | |
import System.IO (IO,putStrLn) | |
import Data.String (String) | |
import Text.Show (show,Show) | |
import Data.Maybe (maybe) | |
import Data.Function ( (.), ($)) | |
import Data.Eq ((==)) | |
data Employee = Employee | |
{ | |
id' :: Int | |
} | |
instance Show Employee where | |
show e = "manager with id " <> show (id' e) | |
newtype EmployeeResult = EmployeeResult (Either String Employee) | |
instance Show EmployeeResult where | |
show (EmployeeResult (Left a)) = a | |
show (EmployeeResult (Right v)) = show v | |
findManager :: [Employee] -> Employee -> EmployeeResult | |
findManager es e = | |
maybe (EmployeeResult . Left $ "Could not find " <> show e) (EmployeeResult . Right) (find (\m -> id' m == id' e) es) | |
inputs = [ Employee 1 , Employee 2 , Employee 3 ] | |
main :: IO() | |
main = do | |
putStrLn . show $ findManager inputs (Employee 0) | |
putStrLn . show $ findManager inputs (Employee 25) | |
putStrLn $ "Found " <> show (findManager inputs (Employee 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment