Last active
September 22, 2015 21:04
-
-
Save unscriptable/dd6f952071e8dcb4e352 to your computer and use it in GitHub Desktop.
Implement Visit using phantom types and smart constructors
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
module PhantomVisit ( | |
Visit, | |
CallAhead, | |
Remote, | |
WalkIn, | |
Rsv, | |
callAhead, | |
remote, | |
walkIn, | |
rsv | |
) where | |
import Data.Time | |
data CallAhead | |
data Remote | |
data WalkIn | |
data Rsv | |
data Visit t = Visit { | |
partySize :: Int, | |
enqueuedAt :: UTCTime, | |
quotedWait :: NominalDiffTime, | |
reservedAt :: Maybe UTCTime | |
} | |
deriving (Eq, Show) | |
rsv :: Int -> UTCTime -> NominalDiffTime -> UTCTime -> Visit Rsv | |
rsv ps time qw ra = Visit ps time qw (Just ra) | |
walkIn :: Int -> UTCTime -> NominalDiffTime -> Visit WalkIn | |
walkIn ps time qw = Visit ps time qw Nothing | |
callAhead :: Int -> UTCTime -> NominalDiffTime -> Visit CallAhead | |
callAhead ps time qw = Visit ps time qw Nothing | |
remote :: Int -> UTCTime -> NominalDiffTime -> Visit Remote | |
remote ps time qw = Visit ps time qw Nothing | |
-- serious oversimplification :) | |
calcWait :: Visit a -> NominalDiffTime | |
calcWait v = case reservedAt v of | |
Nothing -> quotedWait v | |
Just _ -> 0 |
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
t <- getCurrentTime | |
let v1 = rsv 3 t (fromIntegral 3) t | |
let v2 = walkIn 3 t (fromIntegral 3) | |
calcWait v1 | |
> 0s | |
calcWait v2 | |
> 3s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment