Skip to content

Commit

Permalink
Add xtd::linq::enumerable examples and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Jan 6, 2025
1 parent 72622ec commit 8698276
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/xtd.core.examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@
* [enumerable_count](linq/enumerable_count/README.md) shows how to use [xtd::linq::enumerable::count](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ae8deab4a1605619cb40cd56504a5b54c) method.
* [enumerable_count2](linq/enumerable_count2/README.md) shows how to use [xtd::linq::enumerable::count](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ad9b2c99b2315f698741d2f45b0d4db92) method.
* [enumerable_count_by](linq/enumerable_count_by/README.md) shows how to use [xtd::linq::enumerable::count_by](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a6cda69ae32d618aa9d31e18133861f45) method.
* [enumerable_default_if_empty](linq/enumerable_default_if_empty/README.md) shows how to use [xtd::linq::enumerable::default_if_empty](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a89cb4c7f69e1af47e2d24c5e4e963925) method.
* [enumerable_default_if_empty2](linq/enumerable_default_if_empty2/README.md) shows how to use [xtd::linq::enumerable::default_if_empty](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a77cd5c4f6280710bd7422cb5bb6ca0d1) method.
* [enumerable_range](linq/enumerable_range/README.md) shows how to use [xtd::linq::enumerable::range](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a2ad0f9812852378c4ed08a50f39396cd) method.
* [enumerable_select](linq/enumerable_select/README.md) shows how to use [xtd::linq::enumerable::select](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ae2df9541f109773859bcaaf42015b8cd) method.
* [enumerable_select2](linq/enumerable_select2/README.md) shows how to use [xtd::linq::enumerable::select](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ae2df9541f109773859bcaaf42015b8cd) method.
Expand Down
2 changes: 2 additions & 0 deletions examples/xtd.core.examples/linq/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ add_projects(
enumerable_count
enumerable_count2
enumerable_count_by
enumerable_default_if_empty
enumerable_default_if_empty2
enumerable_range
enumerable_select
enumerable_select2
Expand Down
2 changes: 2 additions & 0 deletions examples/xtd.core.examples/linq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* [enumerable_count](enumerable_count/README.md) shows how to use [xtd::linq::enumerable::count](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ae8deab4a1605619cb40cd56504a5b54c) method.
* [enumerable_count2](enumerable_count2/README.md) shows how to use [xtd::linq::enumerable::count](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ad9b2c99b2315f698741d2f45b0d4db92) method.
* [enumerable_count_by](enumerable_count_by/README.md) shows how to use [xtd::linq::enumerable::count_by](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a6cda69ae32d618aa9d31e18133861f45) method.
* [enumerable_default_if_empty](enumerable_default_if_empty/README.md) shows how to use [xtd::linq::enumerable::default_if_empty](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a89cb4c7f69e1af47e2d24c5e4e963925) method.
* [enumerable_default_if_empty2](enumerable_default_if_empty2/README.md) shows how to use [xtd::linq::enumerable::default_if_empty](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a77cd5c4f6280710bd7422cb5bb6ca0d1) method.
* [enumerable_range](enumerable_range/README.md) shows how to use [xtd::linq::enumerable::range](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a2ad0f9812852378c4ed08a50f39396cd) method.
* [enumerable_select](enumerable_select/README.md) shows how to use [xtd::linq::enumerable::select](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ae2df9541f109773859bcaaf42015b8cd) method.
* [enumerable_select2](enumerable_select2/README.md) shows how to use [xtd::linq::enumerable::select](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#ae2df9541f109773859bcaaf42015b8cd) method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <xtd/xtd>

using namespace xtd;
using namespace xtd::collections::generic;

auto main() -> int {
auto fruits = array {"apple", "banana", "mango", "orange", "passionfruit", "grape"};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(enumerable_default_if_empty)
find_package(xtd REQUIRED)
add_sources(README.md src/enumerable_default_if_empty.cpp)
target_type(CONSOLE_APPLICATION)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# enumerable_default_if_empty

Shows how to use [xtd::linq::enumerable::default_if_empty](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a89cb4c7f69e1af47e2d24c5e4e963925) method.

## Sources

[src/enumerable_default_if_empty.cpp](src/enumerable_default_if_empty.cpp)

[CMakeLists.txt](CMakeLists.txt)

## Build and run

Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:

```cmake
xtdc run
```

## Output

```
pet {name=Barley, age=8}
pet {name=Boots, age=4}
pet {name=Whiskers, age=1}
pet {name=, age=0}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <xtd/xtd>

using namespace xtd;

struct pet {
string name;
int age = 0;

friend bool operator==(const pet& lhs, const pet& rhs) noexcept {
return lhs.name == rhs.name && lhs.age == rhs.age;
}
};

auto main() -> int {
auto pets1 = array<pet> {
{.name = "Barley", .age = 8},
{.name = "Boots", .age = 4},
{.name = "Whiskers", .age = 1}
};
for (const auto& [name, age] : pets1.default_if_empty())
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();

auto pets2 = array<pet> {};
for (const auto& [name, age] : pets2.default_if_empty())
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();
}

// This code produces the following output :
//
// pet {name=Barley, age=8}
// pet {name=Boots, age=4}
// pet {name=Whiskers, age=1}
//
// pet {name=, age=0}
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(enumerable_default_if_empty2)
find_package(xtd REQUIRED)
add_sources(README.md src/enumerable_default_if_empty2.cpp)
target_type(CONSOLE_APPLICATION)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# enumerable_default_if_empty2

Shows how to use [xtd::linq::enumerable::default_if_empty](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a77cd5c4f6280710bd7422cb5bb6ca0d1) method.

## Sources

[src/enumerable_default_if_empty2.cpp](src/enumerable_default_if_empty2.cpp)

[CMakeLists.txt](CMakeLists.txt)

## Build and run

Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:

```cmake
xtdc run
```

## Output

```
pet {name=Barley, age=8}
pet {name=Boots, age=4}
pet {name=Whiskers, age=1}
pet {name=Default pet, age=0}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <xtd/xtd>

using namespace xtd;

struct pet {
string name;
int age = 0;

static pet default_pet;

friend bool operator==(const pet& lhs, const pet& rhs) noexcept {
return lhs.name == rhs.name && lhs.age == rhs.age;
}
};

pet pet::default_pet {.name = "Default pet", .age = 0};

auto main() -> int {
auto pets1 = array<pet> {
{.name = "Barley", .age = 8},
{.name = "Boots", .age = 4},
{.name = "Whiskers", .age = 1}
};
for (const auto& [name, age] : pets1.default_if_empty(pet::default_pet))
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();

auto pets2 = array<pet> {};
for (const auto& [name, age] : pets2.default_if_empty(pet::default_pet))
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();
}

// This code produces the following output :
//
// pet {name=Barley, age=8}
// pet {name=Boots, age=4}
// pet {name=Whiskers, age=1}
//
// pet {name=Default pet, age=0}
//
6 changes: 6 additions & 0 deletions src/xtd.core/include/xtd/linq/enumerable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ namespace xtd {
/// @tparam source_t The type of the elements of source.
/// @param source The sequence to return a default value for if it is empty.
/// @return An xtd::collection::generic::ienumerable <type_t> that contains default_value if source is empty; otherwise, source.
/// @par Examples
/// The following code example demonstrates how to use xtd::linq::enumerable::default_if_empty <source_t>(const ienumerable <source_t>&) to return a default value if a sequence is empty.
/// @include enumerable_default_if_empty.cpp
template <typename source_t>
static const ienumerable<source_t>& default_if_empty(const ienumerable<source_t>& source) noexcept {
return default_if_empty(source, source_t {});
Expand All @@ -504,6 +507,9 @@ namespace xtd {
/// @param source The sequence to return a default value for if it is empty.
/// @param default_value The value to return if the sequence is empty.
/// @return An xtd::collection::generic::ienumerable <type_t> that contains default_value if source is empty; otherwise, source.
/// @par Examples
/// The following code example demonstrates how to use xtd::linq::enumerable::default_if_empty <source_t>(const ienumerable <source_t>&, const source_t&) to return a default value if a sequence is empty.
/// @include enumerable_default_if_empty2.cpp
template <typename source_t>
static const ienumerable<source_t>& default_if_empty(const ienumerable<source_t>& source, const source_t& default_value) noexcept {
static thread_local auto result = enumerable_collection<source_t> {};
Expand Down

0 comments on commit 8698276

Please sign in to comment.