Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Haskell playground created by essic - https://repl.it/@essic/Haskell-playground
{-# 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
You can’t perform that action at this time.