Guest User

Untitled

a guest
Jun 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. {-# OPTIONS_GHC -Wall #-}
  2.  
  3. -- An example of representing a connection
  4.  
  5. -- Types
  6. data ConnInfo = ConnInfo
  7. { connState :: ConnState
  8. , server :: String
  9. } deriving (Show)
  10.  
  11. data ConnState
  12. = C1 Connecting
  13. | C2 Connected
  14. | C3 Disconnected
  15. deriving (Show)
  16.  
  17. data Connecting = Connecting
  18. { whenInitiated :: Int
  19. } deriving (Show)
  20.  
  21. data Connected = Connected
  22. { sessionId :: String
  23. , lastPing :: Maybe Int
  24. } deriving (Show)
  25.  
  26. data Disconnected = Disconnected
  27. { whenDisconnected :: Int
  28. } deriving (Show)
  29.  
  30. -- Code
  31. getSessionId :: ConnInfo -> String
  32. getSessionId ci = getSid $ connState ci where
  33. getSid (C1 _) = "Connecting..."
  34. getSid (C2 c) = sessionId c
  35. getSid (C3 _) = "Disconnected"
  36.  
  37. connected :: ConnInfo
  38. connected = ConnInfo
  39. (C2 (Connected "session_id_1234" Nothing))
  40. "api.foo.app"
  41.  
  42. main :: IO ()
  43. main = print $ getSessionId connected -- "session_id_1234"
Add Comment
Please, Sign In to add comment