-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek09Live.hs
213 lines (166 loc) · 5.06 KB
/
Week09Live.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{-# LANGUAGE InstanceSigs #-}
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module Week09Live where
import Control.Concurrent (forkIO, MVar, newEmptyMVar, putMVar, takeMVar)
import Prelude hiding (mapM)
import Data.Traversable (for)
import Network.HTTP ( simpleHTTP
, getRequest
, getResponseBody
)
import Week08 (Parser, runParser, JSON (..), parseJSON)
{- WEEK 09 : DATA DEPENDENCIES and APPLICATIVE FUNCTORS -}
{- Part 9.1 : Sequences of Actions -}
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
mapM f [] = return []
mapM f (x:xs) = do
y <- f x
ys <- mapM f xs
return (y : ys)
-- (>>=) : Monad m => m a -> (a -> m b) -> m b
ap :: Monad m => m (a -> b) -> m a -> m b
ap mf ma = do
f <- mf
a <- ma
return (f a)
-- mapM_v2 :: forall m a b. Monad m => (a -> m b) -> [a] -> m [b]
-- mapM_v2 f [] = return []
-- mapM_v2 f (x : xs)
-- = (return (:) :: m (b -> [b] -> [b]))
-- `ap` (f x :: m b)
-- `ap` (mapM f xs :: m [b])
-- (:) (f x) (map f x)
{-
class Functor m => Applicative m where
pure :: a -> m a
(<*>) :: m (a -> b) -> m a -> m b
-}
mapA :: Applicative f => (a -> f b) -> [a] -> f [b]
mapA f [] = pure []
mapA f (x : xs) = (:) <$> f x <*> mapA f xs
{- Part 9.2 : Applicative -}
-- Type class
{- Part 9.3 : Data Dependencies and Parallelism -}
-- Request/response
type Request = String
type Response = String
-- Fetch
data Fetch a
= Done a
| Fetch [Request] ([Response] -> Fetch a)
instance Show a => Show (Fetch a) where
show (Done a) = "(Done " ++ show a ++ ")"
show (Fetch reqs _) = "(Fetch " ++ show reqs ++ " <continues...>)"
makeRequest :: Request -> Fetch Response
makeRequest url = Fetch [url] (\[resp] -> Done resp)
getField :: JSON -> String -> JSON
getField (Object fields) nm =
case lookup nm fields of
Nothing -> Null
Just v -> v
getField _ _ =
Null
getString :: JSON -> String
getString (String s) = s
getString _ = "ERROR"
makeJSONRequest :: Request -> Fetch String
makeJSONRequest url =
do resp <- makeRequest url
case runParser parseJSON resp of
Nothing -> return "ERROR"
Just (_, json) -> return (getString (getField json "title"))
-- Monad
instance Monad Fetch where
Done x >>= k = k x
Fetch reqs c >>= k = Fetch reqs (\resps -> c resps >>= k)
-- Applicative
instance Applicative Fetch where
pure = Done
Done f <*> Done x = Done (f x)
Done f <*> Fetch reqsr cr
= Fetch reqsr (\ respr -> fmap f (cr respr))
Fetch reqsl cl <*> Done x
= Fetch reqsl (\ respl -> fmap (\ f -> f x) (cl respl))
Fetch reqsl cl <*> Fetch reqsr cr
= Fetch (reqsl ++ reqsr) (\ resplr ->
let (respl, respr) = splitAt (length reqsl) resplr in
let left = cl respl in
let right = cr respr in
left <*> right)
instance Functor Fetch where
fmap f mx = pure f <*> mx
-- runFetch :: Fetch a -> IO a
{- PART 9.4 : Concurrency and Communication -}
{-
forkIO
-}
{- type MVar a
newEmptyMVar :: IO (MVar a)
putMVar :: MVar a -> a -> IO ()
takeMVar :: MVar a -> IO a
-}
-- Logger
data LogMsg
= Message String
| Stop
deriving Show
loggerMain :: MVar LogMsg -> Int -> IO ()
loggerMain inbox count =
do msg <- takeMVar inbox
case msg of
Message msg ->
do putStrLn ("LOG(" ++ show count ++ "): " ++ msg)
loggerMain inbox (count+1)
Stop ->
do putStrLn "LOG STOPPED"
return ()
startLogger :: IO (MVar LogMsg)
startLogger =
do ch <- newEmptyMVar
forkIO (loggerMain ch 0)
return ch
logMsg :: MVar LogMsg -> String -> IO ()
logMsg log msg = putMVar log (Message msg)
logStop :: MVar LogMsg -> IO ()
logStop log = putMVar log Stop
type Logger = MVar LogMsg
-- doRequest
doRequest :: Logger -> Request -> IO Response
doRequest log url =
do log `logMsg` ("Requesting " ++ url)
httpResp <- simpleHTTP (getRequest url)
body <- getResponseBody httpResp
log `logMsg` ("Request " ++ url ++ " finished")
return body
-- http://jsonplaceholder.typicode.com/todos/12
-- parMapM
parMapM :: (a -> IO b) -> [a] -> IO [b]
parMapM f xs = do
mboxes <- mapM (\x -> do m <- newEmptyMVar
forkIO (do y <- f x
putMVar m y)
return m)
xs
mapM takeMVar mboxes
runFetch :: Logger -> Fetch a -> IO a
runFetch log (Done a) = return a
runFetch log (Fetch reqs k) =
do resps <- parMapM (doRequest log) reqs
runFetch log (k resps)
getTodo :: Int -> Fetch String
getTodo n = makeJSONRequest ("http://jsonplaceholder.typicode.com/todos/" ++ show n)
getTodos1 :: Fetch (String, String, String)
getTodos1 =
do todo1 <- getTodo 234
todo2 <- getTodo 123
todo3 <- getTodo 12
return (todo1, todo2, todo3)
getTodos2 :: Fetch (String, String, String)
getTodos2 =
(,,) <$> getTodo 234 <*> getTodo 123 <*> getTodo 12
runFetchWithLogger :: Fetch a -> IO a
runFetchWithLogger job =
do log <- startLogger
result <- runFetch log job
logStop log
return result