Skip to content

Instantly share code, notes, and snippets.

@essic
Created January 4, 2018 16:55
Show Gist options
  • Save essic/a0931c71f6a12d7cc7c41cd478802188 to your computer and use it in GitHub Desktop.
Save essic/a0931c71f6a12d7cc7c41cd478802188 to your computer and use it in GitHub Desktop.
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