-
Notifications
You must be signed in to change notification settings - Fork 10
/
list5.cpp
55 lines (47 loc) · 966 Bytes
/
list5.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
#include <iostream>
#include <list>
#include <utility>
template<typename T>
std::ostream& operator<<(std::ostream& os,const std::list<T>& l)
{
os<<"[";
for(const auto& x:l)os<<x<<" ";
os<<"]";
return os;
}
template<typename T>
struct list:std::list<T>
{
using base=std::list<T>;
using base::base;
list(const T& x):base(1,x){} // compatibility with mreturn
};
list<int> decinc(int x)
{
return {x-1,x+1};
}
template<template<typename> class M,typename T>
M<T> mreturn(const T& x)
{
return x;
}
template<typename T,typename F>
auto operator>>=(const list<T>& l, F f)
{
decltype(f(std::declval<T>())) ret={};
for(const auto& t:l)ret.splice(ret.end(),f(t));
return std::move(ret);
}
#define DO(var,monad,body) \
((monad)>>=[=](const auto& var){ \
return body; \
})
int main()
{
auto res=
DO(x,list<int>({0,1,2}),
DO(y,list<int>({1,2,3}),
mreturn<list>(x+y)
));
std::cout<<res<<"\n";
}