Created
March 31, 2016 12:39
-
-
Save meditans/0d44a897c5cacb0923458bf4a1d7a15b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module Main where | |
import Control.Lens | |
import Control.Monad.State.Lazy (State (..), get, runState) | |
---------------------------------------------------- Data Types | |
type Message = String | |
data Product = Product { _name :: String | |
, _price :: Int | |
} deriving (Show, Eq) | |
data VendingMachine = VendingMachine | |
{ _products :: [Product] | |
, _moneyInside :: Int | |
, _messageOnDisplay :: Message | |
} deriving (Show) | |
makeLenses ''Product | |
makeLenses ''VendingMachine | |
coffee = Product "coffee" 3 | |
initVM = VendingMachine [] 0 "init" | |
---------------------------------------------------- VM Commands | |
vmInsertMoney :: Int -> State VendingMachine () | |
vmInsertMoney amount = moneyInside += amount | |
vmAddProduct :: Product -> State VendingMachine () | |
vmAddProduct newProduct = do | |
products %= (newProduct:) | |
messageOnDisplay .= "Thanks" | |
vmSelectProduct :: Product -> State VendingMachine (Either Message Product) | |
vmSelectProduct p = do | |
(VendingMachine ps money _) <- get | |
if p `elem` ps && money `isEnoughFor` p | |
then do moneyInside -= (p^.price) | |
return (Right p) | |
else do return (Left "Not enough money") | |
isEnoughFor :: Int -> Product -> Bool | |
amount `isEnoughFor` product = amount >= product^.price | |
----------------------------------------------------------- Demo | |
vmDemo :: State VendingMachine (Either Message Product) | |
vmDemo = do vmAddProduct coffee | |
vmInsertMoney 5 | |
vmInsertMoney 10 | |
vmSelectProduct coffee | |
main :: IO () | |
main = print $ runState vmDemo initVM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment