pilu/src/Medicine.hs

33 lines
824 B
Haskell

{-# LANGUAGE NamedFieldPuns #-}
module Medicine (
Medicine(..)
, MedicineName
, Pharmacy
, pharmacy
) where
import CSV (Row(..))
import Data.Map (Map)
import qualified Data.Map as Map (empty, insert, lookup)
import Text.ParserCombinators.Parsec.Error (Message(..))
type MedicineName = String
data Medicine = Medicine {
name :: MedicineName
, content :: Float
, minStock :: Float
} deriving (Show)
instance Row Medicine where
fromRow assoc = Medicine <$>
get "name"
<*> (read <$> get "content")
<*> (read <$> get "minStock")
where
get key = maybe (Left $ Expect key) Right $ Map.lookup key assoc
type Pharmacy = Map MedicineName Medicine
pharmacy :: [Medicine] -> Pharmacy
pharmacy = foldr (\medicine -> Map.insert (name medicine) medicine) Map.empty