This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta-cpp.cpp
More file actions
214 lines (167 loc) · 5.4 KB
/
meta-cpp.cpp
File metadata and controls
214 lines (167 loc) · 5.4 KB
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
#include "types.hpp"
#include "algorithms.hpp"
#include "arithmetics.hpp"
#include "stream.hpp"
#ifdef _MSC_VER
#define SHOW(...) __VA_ARGS__
#else
#define SHOW(...) show<__VA_ARGS__>
template <typename...>
concept show = true;
#endif
#pragma region test lists
using test_list_1 =
L(A(9),
L(A(1),
L(A(2),
A(4))),
L(A(3),
A(4)),
L(A(7),
L(A(6),
L(A(2),
A(4)))),
A(10));
using test_list_2 =
L(
L(A(1), A(2)),
L(A(2), A(3)),
L(A(3), A(4)));
using test_list_3 = Ret(push_back, L(L(A(4), A(5), A(7)), A(5)), test_list_2);
using test_list_4 =
L(
L(True, True),
L(True, False),
L(False, True),
L(False, False));
#pragma endregion
DEFN_UNARY_FUN(twice, x, A(2 * Value(x)));
DEFN_UNARY_FUN(odd, x, A(Value(x) % 2 != 0));
using half = C(Div, L(id, A(2)));
// #define ALGOR_TEST
#ifdef ALGOR_TEST
SHOW(Ret(L(peek_back, peek_front), test_list_2));
SHOW(Ret(L(pop_back, pop_front), test_list_2));
SHOW(Ret(join, test_list_1, test_list_2));
SHOW(Ret(deep_map, L(Inc, Neg, Dec), test_list_1));
SHOW(Ret(map, swap, test_list_2));
// TODO: 二元函数的 map 应该科里化
// @Deprecated(这就是为什么不允许只有一个元素的和空的表)
SHOW(Ret(map, L(Add, Sub, Mul, Div), test_list_2));
SHOW(Ret(map, L(And, Or, Xor, NAnd, NOr), test_list_2));
SHOW(Ret(deep_map, Not, test_list_2));
SHOW(Ret(map, L(And, Or, Xor, NAnd, NOr), test_list_4));
SHOW(Ret(deep_map, Not, test_list_4));
SHOW(Ret(map, L(twice, half), Ret(flatten, test_list_2)));
using insert_999_to_list_2 = Ret(bind, Ret(bind, insert_l, A(999), A(0)), test_list_2, A(0));
SHOW(Ret(map, insert_999_to_list_2, L(A(-1), A(0), A(1), A(2), A(3), A(4))));
using remove_from_list_2 = Ret(bind, remove_l, test_list_2, A(0));
SHOW(Ret(map, remove_from_list_2, L(A(-1), A(0), A(1), A(2), A(3))));
using remove_from_list_3 = Ret(bind, remove_l, test_list_3, A(0));
SHOW(Ret(map, remove_from_list_2, L(A(-1), A(0), A(1), A(2), A(3))));
using replace_list_2_with_999 = Ret(bind, Ret(bind, replace_l, A(999), A(0)), test_list_2, A(0));
SHOW(Ret(map, replace_list_2_with_999, L(A(-1), A(0), A(1), A(2), A(3), A(4))));
using find_odds1 = C(Ret(bind, filter, odd, A(0)), bind::take_as_1_arg, flatten);
SHOW(Ret(find_odds1, test_list_1));
SHOW(Ret(find_odds1, test_list_1));
SHOW(Ret(find_odds1, test_list_1));
using find_odds2 = C(Ret(bind, deep_filter, odd, A(0)), bind::take_as_1_arg);
SHOW(Ret(find_odds2, test_list_1));
SHOW(Ret(find_odds2, test_list_2));
SHOW(Ret(find_odds2, test_list_3));
using find_odds3 = C(deep_filter, L(Ret(wrap, odd), id));
SHOW(Ret(find_odds3, test_list_1));
SHOW(Ret(find_odds3, test_list_2));
SHOW(Ret(find_odds3, test_list_3));
#endif
#pragma region test streams
using integer_seq = S(Inc, A(1));
using even_seq = Ret(map_s, twice, integer_seq);
using fibonacci_seq = S(Add, A(1), A(1));
using odd_seq = Ret(filter_s, odd, integer_seq);
using odd_fibonacci_seq = Ret(filter_s, odd, fibonacci_seq);
struct mk_prime_finder
{
FUNC_HEAD_THROW(mk_prime_finder);
template <is::arg N, is::delayed finder>
struct apply_on<L(N, force::avoid<finder>)>
{
using last_prime = Id(N);
using s = L(last_prime, finder);
using p_aliquant = C(
Ret(
bind,
cast,
bool, A(1)),
Ret(
bind,
Mod,
last_prime, A(1)));
using temp_s = L(INC(last_prime), Ret(peek_back, s));
using new_finder =
Ret(peek_back,
Ret(
filter_s,
p_aliquant,
temp_s));
using next_list = F(new_finder);
using next_prime = Ret(peek_front, next_list);
using ret = L(Id(next_prime), D(mk_prime_finder, L(next_prime, force::avoid<Ret(peek_back, next_list)>)));
};
};
using prime_seq =
L(A(2), D(mk_prime_finder, L(A(2), force::avoid<Ret(peek_back, integer_seq)>)));
#pragma endregion
//#define STREAM_TEST
#ifdef STREAM_TEST
SHOW(Ret(pop_back, Ret(calc, integer_seq, A(9)));
SHOW(Ret(pop_back, Ret(calc, even_seq, A(9))));
SHOW(Ret(pop_back, Ret(calc, fibonacci_seq, A(9))));
SHOW(Ret(pop_back, Ret(calc, odd_seq, A(9))));
SHOW(Ret(pop_back, Ret(calc, odd_fibonacci_seq, A(9))));
SHOW(Ret(pop_back, Ret(calc, prime_seq, A(30))));
#endif
// using delayed_inc = C(apply, Ret(wrap, L(Inc, A(0))));
// delayed_inc;
// Ret(delayed_inc, void);
#include <iostream>
#include <tuple>
#include <sstream>
template <typename... Ts>
struct get::__value<L(Ts...)>
{
SIC auto ret = std::make_tuple(__value<Ts>::ret...);
};
struct
{
private:
std::stringstream ss;
template <typename... Ts, size_t... Is>
void __tuple2str(const std::tuple<Ts...> &t, std::index_sequence<Is...>)
{
ss.clear();
ss << "(";
(..., (ss << (Is == 0 ? "" : ", "), operator()(std::get<Is>(t))));
ss << ")";
}
public:
template <typename T>
std::string operator()(T item)
{
ss.clear();
ss << item;
return ss.str();
}
template <typename... Ts>
std::string operator()(const std::tuple<Ts...> &t)
{
__tuple2str(t, std::make_index_sequence<sizeof...(Ts)>());
return ss.str();
}
} tuple2str;
int main()
{
auto primes = Value(Ret(C(pop_back, calc), prime_seq, A(9)));
std::cout << tuple2str(primes) << std::endl;
return 0;
}