Guest User

Untitled

a guest
May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. 18 data ClientSession = ClientSession
  2. 19 { sec :: Int
  3. 20 , nsec :: Int
  4. 21 , username :: String
  5. 22 , dbid :: Integer
  6. 23 , uuid :: Int
  7. 24 , prand :: Int
  8. 25 } deriving (Ord, Eq)
  9. 26
  10. 27 instance Show ClientSession where
  11. 28 show (ClientSession
  12. 29 { sec = s
  13. 30 , nsec = ns
  14. 31 , username = un
  15. 32 , dbid = db
  16. 33 , uuid = ud
  17. 34 , prand = pr}) = intercalate "|" ls
  18. 35 where ls = [show s, show ns, un, show db, show ud, show pr]
  19.  
  20. 37 fromString :: String -> Maybe ClientSession
  21. 38 fromString ss = fromParts $ endBy "|" ss
  22. ...
  23. 74 fromParts :: [String] -> Maybe ClientSession
  24. 75 fromParts (s:ns:un:db:ud:pr:[])
  25. 76 = newSessionM (readMaybe s) (readMaybe ns) (Just un) (readMaybe db) (readMaybe ud) (readMaybe pr)
  26. 77 fromParts _ = Nothing
  27. 78
  28. 79 newSessionM :: Maybe Int
  29. 80 -> Maybe Int
  30. 81 -> Maybe String
  31. 82 -> Maybe Integer
  32. 83 -> Maybe Int
  33. 84 -> Maybe Int
  34. 85 -> Maybe ClientSession
  35. 86 newSessionM (Just s)
  36. 87 (Just ns)
  37. 88 (Just un)
  38. 89 (Just db)
  39. 90 (Just ud)
  40. 91 (Just pr) = return $ newSession s ns un db ud pr
  41. 92 newSessionM _ _ _ _ _ _ = Nothing
  42. 93
  43. 94 newSession :: Int -> Int -> String -> Integer -> Int -> Int -> ClientSession
  44. 95 newSession s ns un db ud pr = ClientSession
  45. 96 { sec = s
  46. 97 , nsec = ns
  47. 98 , username = un
  48. 99 , dbid = db
  49. 100 , uuid = ud
  50. 101 , prand = pr}
  51.  
  52. import Control.Applicative
  53. import Data.Char (isDigit)
  54. import Data.Functor
  55. import Data.List
  56. import Text.ParserCombinators.ReadP as P
  57.  
  58. parseInt :: (Read a, Integral a) => ReadP a
  59. parseInt = read <$> munch1 isDigit <* P.optional (char '|')
  60.  
  61. parseNotBar :: ReadP String
  62. parseNotBar = munch (/= '|') <* P.optional (char '|')
  63.  
  64. instance Read ClientSession where
  65. readsPrec _ = readP_to_S parseClientSession
  66. where
  67. parseClientSession :: ReadP ClientSession
  68. parseClientSession =
  69. ClientSession <$> parseInt <*> parseInt <*> parseNotBar
  70. <*> parseInt <*> parseInt <*> parseInt
  71.  
  72. instance Read ClientSession where
  73. readsPrec _ = readP_to_S $
  74. ClientSession <$> parseInt <*> parseInt <*> parseNotBar
  75. <*> parseInt <*> parseInt <*> parseInt
  76.  
  77. newSessionM s ns un db ud pr
  78. = ClientSession <$> s <*> ns <*> un <*> db <*> ud <*> pr
  79.  
  80. parseClientSession =
  81. newSessionM parseInt parseInt parseNotBar
  82. parseInt parseInt parseInt
Add Comment
Please, Sign In to add comment