Skip to content

Commit 0b88262

Browse files
committed
Add forward_iterable concept example
1 parent 802570e commit 0b88262

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

examples/xtd.core.examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
* [comparable](concepts/comparable/README.md) shows how to use how to use [xtd::comparable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1comparable.html) concept.
5252
* [enumerable](concepts/enumerable/README.md) shows how to use how to use [xtd::collections::generic::enumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1enumerable.html) concept.
53+
* [forward_iterable](concepts/forward_iterable/README.md) shows how to use how to use [xtd:: forward_iterable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1 forward__iterable.html) concept.
5354
* [stringable](concepts/stringable/README.md) shows how to use how to use [xtd::stringable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1stringable.html) concept.
5455

5556
## [Configuration](configuration/README.md)

examples/xtd.core.examples/concepts/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ find_package(xtd REQUIRED)
66
add_projects(
77
comparable
88
enumerable
9+
forward_iterable
910
stringable
1011
)
1112

examples/xtd.core.examples/concepts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [comparable](comparable/README.md) shows how to use how to use [xtd::comparable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1comparable.html) concept.
66
* [enumerable](enumerable/README.md) shows how to use how to use [xtd::collections::generic::enumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1enumerable.html) concept.
7+
* [forward_iterable](forward_iterable/README.md) shows how to use how to use [xtd:: forward_iterable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1 forward__iterable.html) concept.
78
* [stringable](stringable/README.md) shows how to use how to use [xtd::stringable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1stringable.html) concept.
89

910
## Build and run any project
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(forward_iterable)
4+
find_package(xtd REQUIRED)
5+
add_sources(README.md src/forward_iterable.cpp)
6+
target_type(CONSOLE_APPLICATION)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# enumerable
2+
3+
Shows how to use [xtd::collections::generic::enumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1enumerable.html) concept.
4+
5+
## Sources
6+
7+
* [src/enumerable.cpp](src/enumerable.cpp)
8+
* [CMakeLists.txt](CMakeLists.txt)
9+
10+
## Build and run
11+
12+
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:
13+
14+
```cmake
15+
xtdc run
16+
```
17+
18+
## Output
19+
20+
```
21+
2
22+
4
23+
6
24+
8
25+
10
26+
18
27+
21
28+
24
29+
27
30+
30
31+
44
32+
48
33+
52
34+
56
35+
60
36+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <xtd/xtd>
2+
3+
auto int_generator() -> enumerable_generator<int> {
4+
co_yield 15;
5+
co_yield 11;
6+
co_yield 13;
7+
co_yield 14;
8+
co_yield 12;
9+
}
10+
11+
template <forward_iterable forward_iterable_type>
12+
[[nodiscard]] auto from_forward_iterable(forward_iterable_type&& iterable) -> enumerable_generator<forward_iterable_value_type<forward_iterable_type>> {
13+
for (const auto& item : iterable)
14+
co_yield item;
15+
}
16+
17+
auto main() -> int {
18+
for (auto item : from_forward_iterable(std::vector {5, 1, 3, 4, 2}).select([](auto v) {return v * 2;}).order())
19+
println("{,2}", item);
20+
for (auto item : from_forward_iterable(list {10, 6, 8, 9, 7}).select([](auto v) {return v * 3;}).order())
21+
println("{,2}", item);
22+
for (auto item : from_forward_iterable(int_generator()).select([](auto v) {return v * 4;}).order())
23+
println("{,2}", item);
24+
}
25+
26+
// This code produces the following output :
27+
//
28+
// 2
29+
// 4
30+
// 6
31+
// 8
32+
// 10
33+
// 18
34+
// 21
35+
// 24
36+
// 27
37+
// 30
38+
// 44
39+
// 48
40+
// 52
41+
// 56
42+
// 60

0 commit comments

Comments
 (0)