-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy path金兔兔.cpp
41 lines (37 loc) · 783 Bytes
/
金兔兔.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
//!gcc !clang
import std;
template<typename T>
struct Lazy
{
using FnType = std::function<void(T&)>;
std::vector<T> v;
mutable FnType map;
Lazy(std::vector<T> v) noexcept :v(v), map([](T& x) {}) {}
Lazy(std::vector<T> v, FnType map) noexcept :v(v), map(map) {}
~Lazy() noexcept
{
for (auto& x : v)
map(x);
}
const Lazy& operator|(FnType map2) const noexcept
{
auto old = map;
map = [map2, old](T& x) {
old(x);
map2(x);
};
return *this;
}
};
template<typename T, typename F>
Lazy<T> operator|(const std::vector<T>& c, const F& map) noexcept
{
return Lazy{ c } | std::function<void(T&)>(map);
}
int main()
{
std::vector v{ 1, 2,3 };
std::function f{ [](const int& i) {std::cout << i << ' '; } };
auto f2 = [](int& i) {i *= i; };
v | f2 | f;
}