-
Notifications
You must be signed in to change notification settings - Fork 10
/
list3.cpp
50 lines (43 loc) · 954 Bytes
/
list3.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
#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);
}
template<template<typename> class M,typename T,typename F>
auto transform(const M<T>& m,F f)
{
return m>>=[=](const auto& x){return mreturn<M>(f(x));};
}
int main()
{
std::cout<<transform(list<int>{0,1,2},[](int x){return x+1;})<<"\n";
}