-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
326 lines (274 loc) · 7.92 KB
/
main.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**************************************************
**
** Copyright (c) 2015 TeamDrive Systems GmbH
**
** See the file LICENSE.txt for copying permission.
**
***************************************************/
#include <QCoreApplication>
#include <QDebug>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>
#include <QSet>
#include <QHash>
#include <QList>
#include <htmlcxx/html/ParserSax.h>
#include "cont.h"
typedef Cont<QSet<QUrl>> UrlContinuation;
typedef Cont<QNetworkReply*> ReplyContinuatoin;
void testBasics();
void testOptional();
void testSequence();
void testGrabLinks(QCoreApplication& a);
void testMapM();
void testLoop();
void testCallCC();
void testException();
ReplyContinuatoin waitForReplyFinished(QNetworkReply *rep);
UrlContinuation grabLinks(const QUrl& url, int depth);
QSet<QUrl> scrapUrls(const QUrl& base, QIODevice* dev);
QNetworkAccessManager *nam = 0;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Continuation monad
nam = new QNetworkAccessManager();
// testBasics();
// testOptional();
// testSequence();
// testGrabLinks(a);
// testMapM();
//testLoop();
// testCallCC();
testException();
return 0;
// return a.exec();
}
UrlContinuation grabLinks(const QUrl& url, int depth)
{
if (depth <= 0)
return pure<Cont>(QSet<QUrl>() << url);
return waitForReplyFinished(nam->get(QNetworkRequest(url))) >>= [url, depth](QNetworkReply* rep) -> UrlContinuation {
QSet<QUrl> urls = scrapUrls(url, rep);
return mapM<Cont, QSet, QUrl, QSet<QUrl>>([depth](QUrl u){
// qDebug() << "grabLinks(u, depth-1)" << u;
return grabLinks(u, depth-1);
}, urls) >>=[](QSet<QSet<QUrl>> uurls){
return pure<Cont>(join<QSet>(uurls));
};};
}
QSet<QUrl> scrapUrls(const QUrl& base, QIODevice* dev)
{
using namespace htmlcxx::HTML;
struct Handler: ParserSax {
QSet<QUrl>& m_Urls;
const QUrl& m_Base;
Handler(const QUrl& b, QSet<QUrl>& u): m_Urls(u), m_Base(b){}
void foundTag(Node node, bool) {
if (node.tagName() == "a") {
node.parseAttributes();
// qDebug() << "Read Start Tag : " << QString::fromStdString(node.tagName()) << QString::fromStdString(node.attribute("href").second);
auto a = node.attribute("href").second;
if (a != ""){
m_Urls << m_Base.resolved(QUrl(QString::fromStdString(a)));
}
}
}
};
QSet<QUrl> urls;
Handler xml(base, urls);
xml.parse(QString::fromUtf8(dev->readAll()).toStdString());
return urls;
}
ReplyContinuatoin waitForReplyFinished(QNetworkReply *rep)
{
return ReplyContinuatoin (
[rep](ReplyContinuatoin::Inner inner) -> void {
if (rep->isFinished()) {
inner(rep);
}
QObject* guard = new QObject();
qDebug() << rep->request().url();
QObject::connect(rep, &QNetworkReply::finished, guard, [guard, inner, rep]() -> void {
// qDebug() << "waitForReplyFinished";
delete guard;
rep->deleteLater();
inner(rep);
});
QTimer::singleShot(10000, guard, [guard, rep](){
qDebug() << "Killing request after 10s" << rep->request().url();
rep->abort();
});
}
);
}
void testBasics()
{
IntCont myCont = pure<Cont>(1) >>= [](int i) {
return pure<Cont>(i + 1);
};
qDebug() << ">>=" << evalWithEventLoop(myCont);
qDebug() << "join" << evalWithEventLoop(join<Cont>(pure<Cont>(pure<Cont>(1))));
IntCont myCont2 = (pure<Cont>(1)) >> []() -> IntCont {
return pure<Cont>(42);
};
qDebug() << ">>" << evalWithEventLoop(myCont2);
}
void testOptional()
{
// Maybe Monad
qDebug() << pure<boost::optional>(42);
qDebug() << pure<boost::optional>(42);
qDebug() << (pure<boost::optional>(42) >>= [](int i) -> boost::optional<int> {return boost::optional<int>(i);});
qDebug() << (pure<boost::optional>(42) >> []() -> boost::optional<int> {return boost::optional<int>();});
qDebug() << join<boost::optional>(pure<boost::optional>(pure<boost::optional>(42))).get();
}
void testSequence()
{
// sequence
qDebug() << sequence<boost::optional, QList>(QList<boost::optional<int>>{ boost::optional<int>(3), boost::optional<int>(4) , boost::optional<int>(5) });
qDebug() << sequence<boost::optional, QList>(QList<boost::optional<int>>{ boost::optional<int>(3), boost::optional<int>() , boost::optional<int>(5) });
// qDebug() << sequence<boost::optional, std::vector, int>(std::vector<boost::optional<int>> { boost::optional<int>(3), boost::optional<int>(4) , boost::optional<int>(5) }); ?
}
void testGrabLinks(QCoreApplication& a)
{
(
grabLinks(QUrl("http://www.teamdrive.com/"), 2) >>= [&a](QSet<QUrl> urls) -> VoidCont {
qDebug() << "urls.len():" << urls;
a.exit();
return pure<Cont>(Unit());
}).evalCont();
}
void testMapM()
{ // mapM
qDebug() << mapM<boost::optional, QList, int, QString>([](int i){
return boost::optional<QString>(QString::number(i * 10, 16));
},QList<int>{1,2,3,4,5,6,7,8,9});
qDebug() << mapM<QList, QList, int, int>([](int i){
return QList<int>() << i << i + 1;
},QList<int>{0,0,0,0,0,0,0});
}
Cont<int> recContCount(int i)
{
if (i > 0) {
return pure<Cont>(Unit()) >> [i](){
return recContCount(i - 1) >>= [](int i){
return pure<Cont>(i + 1);
};};
} else {
return pure<Cont>(0);
}
}
void testLoop()
{
int res = evalWithEventLoop(recContCount(10));
qDebug() << res;
}
void testCallCC()
{
// quux :: Cont r Int
// quux = callCC $ \k -> do
// let n = 5
// k n
// return 25
(callCC<int, int>([](std::function<Cont<int>(int)> k){
return k(5);
return pure<Cont>(25);
}) >>= [](int i){
qDebug() << "5 =" << i;
return pure<Cont>(i);
}).evalCont();
// data SqrtException = LessThanZero deriving (Show, Eq)
// sqrtIO :: (SqrtException -> ContT r IO ()) -> ContT r IO ()
// sqrtIO throw = do
// ln <- lift (putStr "Enter a number to sqrt: " >> readLn)
// when (ln < 0) (throw LessThanZero)
// lift $ print (sqrt ln)
// main = runContT (tryCont sqrtIO (lift . print)) return
enum SqrtException {LessThanZero};
std::function<Cont<int>(std::function<Cont<int>(SqrtException)>)> sqrtIO ([](std::function<Cont<int>(SqrtException)> throwF) -> Cont<int> {
return pure<Cont>(0) >>= [throwF](int i) -> Cont<int> {
if (i <= 0)
return throwF(LessThanZero);
return pure<Cont>(i) >>= [](int i) -> Cont<int> {
qDebug() << "i = " << i;
return pure<Cont>(i);
};};});
tryCont<int, SqrtException>(sqrtIO, std::function<Cont<int>(SqrtException)>([](SqrtException e) -> Cont<int> {
qDebug() << "e";
return pure<Cont>(0);
})).evalCont();
}
void testException()
{
Async<Unit>::raise(new AsyncException()
).tryAsync(
[](AsyncException*) -> Cont<Unit>{
qDebug("ok 1");
return pure<Cont>(Unit());
}
).evalCont();
(
pure<Async>(Unit()) >> [](){
return Async<Unit>::raise(new AsyncException()) >> [](){
return pure<Async>(Unit()) >> [](){
qDebug("error");
return pure<Async>(Unit());
};};}
).tryAsync([](AsyncException*){
qDebug("ok 2");
return pure<Cont>(Unit());
}).evalCont();
#if 0
(
(
Async<Unit>::raise(new AsyncException()
).tryAsync([](AsyncException*){
qDebug("ok 3");
return pure<Cont>(Unit());
})
).tryAsync([](AsyncException*){
qDebug("error");
return pure<Cont>(Unit());
})).evalCont();
(
(
Async<Unit>::raise(new AsyncException())
).tryAsync([](AsyncException*){
qDebug("ok 4");
return Async<Unit>::raise(new AsyncException());
})
).tryAsync([](AsyncException*){
qDebug("ok 5");
return pure<Cont>(Unit());
}).evalCont();
(
Async<Unit>::raise(new AsyncException()) >> [](){
return (
Async<Unit>::raise(new AsyncException())
).tryAsync([](AsyncException*){
qDebug("error!");
return Async<Unit>::raise(new AsyncException());
});}
).tryAsync([](AsyncException*){
qDebug("ok 6");
return pure<Cont>(Unit());
}).evalCont();
(
(
pure<Async>(Unit())
).tryAsync([](AsyncException*){
qDebug("error!");
return Async<Unit>::raise(new AsyncException());
}) >> [](){
return Async<Unit>::raise(new AsyncException());
}
).tryAsync([](AsyncException*){
qDebug("ok 7/7");
return pure<Cont>(Unit());
}).evalCont();
#endif
}