-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_back.cpp
78 lines (67 loc) · 2.21 KB
/
bind_back.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
//#include <fmt/format.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <ranges>
#include <utility>
/// don't open "std", this is for demonstration purposes only
namespace formt { // to format output
auto find_braces(std::string s_p)
{for(int i = 0; i < s_p.size(); i++) {
if(s_p[i] == '{' and s_p[i + 1] == '}') {
return i;
} }
return -1; }
template<typename T>
void print(std::string s, T t) {
int i = find_braces(s);
if(i == -1) {
throw std::runtime_error("Entered variables but no /'{}/'."); }
auto substr_v = s.substr(0, i);
std::cout << substr_v << t;
auto substr_v2 = s.substr(i + 2, (s.size() - i));
std::cout << substr_v2; }
int j = 0; int c = 0;
template<typename T, typename... Ts>
void print(std::string s, T t, Ts... ts) {
auto x = find_braces(s);
j = x;
if(j != -1) {
auto sub_str_v = s.substr(0, j);
std::cout << sub_str_v;
std::cout << t;} else {
throw std::runtime_error("Entered variables but no /'{}/'.");}
auto w = s.substr(x + 2, s.size());
print(w, ts...); } } // formt
namespace lib {
template <typename Func, typename... Param>
auto bind_back(Func &&func, Param &&...param) {
return [func = std::forward<Func>(func),
... param = std::forward<Param>(param)]<typename... Inner>(
Inner &&...inner) -> decltype(auto) {
return std::invoke(func, std::forward<Inner>(inner)..., param...);
};
}
} // namespace std
struct Point {
int x;
int y;
void displace(int x_displacement, int y_displacement) noexcept {
x += x_displacement;
y += y_displacement;
}
Point operator+(Point displacement) const {
return Point{x + displacement.x, y + displacement.y};
}
void print() const { formt::print("{{},{}}\n", x, y); }
};
std::vector<Point> get_data() { return {{1, 2}, {3, 4}, {5, 6}, {42, 78}}; }
int main() {
auto data = get_data();
std::ranges::for_each(data, lib::bind_back(&Point::displace, 6, 5));
std::ranges::for_each(data, &Point::print);
for (const auto &data : get_data() | std::views::transform(lib::bind_back(
std::plus<>{}, Point{7, 6}))) {
data.print();
}
}