Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Last active September 22, 2015 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unscriptable/dd6f952071e8dcb4e352 to your computer and use it in GitHub Desktop.
Save unscriptable/dd6f952071e8dcb4e352 to your computer and use it in GitHub Desktop.
Implement Visit using phantom types and smart constructors
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
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