-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo-rails-monad.hs
61 lines (39 loc) · 1.24 KB
/
two-rails-monad.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module MyRequest where
import Control.Monad
import Control.Applicative
import Control.Monad (liftM, ap)
data Flow a = Success a | Failure String deriving Show
instance Monad Flow where
(Success a) >>= f = f a
Failure a >>= f = Failure a
return a = Success a
fail msg = Failure msg
validRequest = Success "some http request /products"
hackerattack = Success "hacker"
blankrequest = Success ""
flow mrequest = do
content <- mrequest
params <- parsecontent content
validate params
response <- findProducts params
return response
parsecontent content = (return . words) content
validate [] = fail "request is blank"
validate ["hacker"] = fail "Oh Lord! It is an Attack"
validate params = return params
findProducts params = do
--getDBconnection
let result = fromRepository params
return result
fromRepository params = do
if elem "/products" params
then successResponse
else attackResponse
getDBconnection = Failure "Oh Lord! Database connection timeout!!!"
successResponse = ["Hosting", "EmailMkt", "Site Creator", "Cloud", "SMTP", "..."]
attackResponse = ["password: 84848884444", "creditcard: 222"]
instance Functor Flow where
fmap = liftM
instance Applicative Flow where
pure = return
(<*>) = ap