-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
36 lines (31 loc) · 768 Bytes
/
main.cpp
File metadata and controls
36 lines (31 loc) · 768 Bytes
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
#include <cppcoro/generator.hpp>
#include <cppcoro/fmap.hpp>
#include <iostream>
using namespace cppcoro;
using namespace std;
// 1부터 무한히 자연수 생성
auto naturals() -> generator<int>
{
for (int i = 1;; ++i)
co_yield i;
}
int main()
{
// 파이프 조합: naturals() → 제곱으로 변환 → 짝수만 선택
auto squares = naturals()
| fmap([](int x) { return x * x; })
| fmap([](int x) -> optional<int> {
if (x % 2 == 0) return x; // 짝수만 남김
return nullopt; // 나머지는 건너뜀
});
int count = 0;
for (auto v : squares)
{
if (v.has_value())
{
cout << v.value() << " ";
if (++count >= 10) break; // 앞 10개만 출력
}
}
cout << "\n";
}