2019-03-29 19:06:23 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module Medicine (
|
|
|
|
Medicine(..)
|
2019-04-07 18:08:22 +02:00
|
|
|
, MedicineName
|
2019-03-29 19:06:23 +01:00
|
|
|
, 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(..))
|
|
|
|
|
2019-04-07 18:08:22 +02:00
|
|
|
type MedicineName = String
|
|
|
|
|
2019-03-29 19:06:23 +01:00
|
|
|
data Medicine = Medicine {
|
2019-04-07 18:08:22 +02:00
|
|
|
name :: MedicineName
|
2019-04-10 10:44:53 +02:00
|
|
|
, content :: Float
|
2019-04-07 18:08:22 +02:00
|
|
|
, minStock :: Float
|
2019-03-29 19:06:23 +01:00
|
|
|
} deriving (Show)
|
|
|
|
|
|
|
|
instance Row Medicine where
|
|
|
|
fromRow assoc = Medicine <$>
|
|
|
|
get "name"
|
2019-04-10 10:44:53 +02:00
|
|
|
<*> (read <$> get "content")
|
2019-03-29 19:06:23 +01:00
|
|
|
<*> (read <$> get "minStock")
|
|
|
|
where
|
|
|
|
get key = maybe (Left $ Expect key) Right $ Map.lookup key assoc
|
|
|
|
|
2019-04-07 18:08:22 +02:00
|
|
|
type Pharmacy = Map MedicineName Medicine
|
2019-03-29 19:06:23 +01:00
|
|
|
|
|
|
|
pharmacy :: [Medicine] -> Pharmacy
|
2019-04-07 18:08:22 +02:00
|
|
|
pharmacy = foldr (\medicine -> Map.insert (name medicine) medicine) Map.empty
|