pilu/src/Medicine.hs

34 lines
824 B
Haskell
Raw Normal View History

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