Skip to content

Commit

Permalink
Add xtd::linq::enumerable examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Dec 20, 2024
1 parent b41b88f commit cfdb1a7
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/xtd.core.examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_projects(
interfaces
io
keywords
linq
mains
math
media
Expand Down
7 changes: 7 additions & 0 deletions examples/xtd.core.examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@
* [typeof](keywords/typeof/README.md) shows how to use [typeof_](https://gammasoft71.github.io/xtd/reference_guides/latest/group__keywords.html#gafa2ffd9b4a6568b57ab2731bec095d99) keyword.
* [using](keywords/using/README.md) shows how to use [using_](https://gammasoft71.github.io/xtd/reference_guides/latest/group__keywords.html#ga73249a3d4ad48e36724bccf5f3e48f2f) keyword.

## [Linq](linq/README.md)

* [enumerable_aggregate](linq/enumerable_aggregate/README.md) shows how to use [xtd::linq::enumerable::aggregate](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a80ec308a9a8c3b94822405b811270630) method.
* [enumerable_aggregate2](linq/enumerable_aggregate2/README.md) shows how to use [xtd::linq::enumerable::aggregate](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a65cb8dbb360aba65ed73cc16bb8bbe9a) method.
* [enumerable_aggregate3](linq/enumerable_aggregate3/README.md) shows how to use [xtd::linq::enumerable::aggregate](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a65e1b63547102643e7ad9acf9cb56174) method.
* [enumerable_all](linq/enumerable_all/README.md) shows how to use [xtd::linq::enumerable::all](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a3aeb62875d4858df7f852a61ccbb9053) method.

## [Mains](mains/README.md)

* [main](mains/main/README.md) shows how to use [xtd::startup](https://gammasoft71.github.io/xtd/reference_guides/latest/startup_8h.html) method.
Expand Down
11 changes: 11 additions & 0 deletions examples/xtd.core.examples/linq/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)

project(linq)
find_package(xtd REQUIRED)

add_projects(
enumerable_aggregate
enumerable_aggregate2
enumerable_aggregate3
enumerable_all
)
16 changes: 16 additions & 0 deletions examples/xtd.core.examples/linq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Linq examples

[This folder](.) contains guid examples used by [Reference Guide](https://gammasoft71.github.io/xtd/reference_guides/latest/) and more.

* [enumerable_aggregate](enumerable_aggregate/README.md) shows how to use [xtd::linq::enumerable::aggregate](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a80ec308a9a8c3b94822405b811270630) method.
* [enumerable_aggregate2](enumerable_aggregate2/README.md) shows how to use [xtd::linq::enumerable::aggregate](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a65cb8dbb360aba65ed73cc16bb8bbe9a) method.
* [enumerable_aggregate3](enumerable_aggregate3/README.md) shows how to use [xtd::linq::enumerable::aggregate](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a65e1b63547102643e7ad9acf9cb56174) method.
* [enumerable_all](enumerable_all/README.md) shows how to use [xtd::linq::enumerable::all](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1linq_1_1enumerable.html#a3aeb62875d4858df7f852a61ccbb9053) method.

## Build and run any project

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

```shell
xtdc run -t any_project_name
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(enumerable_aggregate)
find_package(xtd REQUIRED)
add_sources(README.md src/enumerable_aggregate.cpp)
target_type(CONSOLE_APPLICATION)
23 changes: 23 additions & 0 deletions examples/xtd.core.examples/linq/enumerable_aggregate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# enumerable_aggregate

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

## Sources

[src/enumerable_aggregate.cpp](src/enumerable_aggregate.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

```
"dog lazy the over jumps fox brown quick the"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <xtd/linq/enumerable>
#include <xtd/println>

using namespace xtd;
using namespace xtd::linq;

auto main() -> int {
auto sentence = "the quick brown fox jumps over the lazy dog"_s;

// Split the string into individual words.
auto words = sentence.split(' ');

// Prepend each word to the beginning of the new sentence to reverse the word order.
auto reversed = enumerable::aggregate(words, [](const string& working_sentence, const string& next) {
return next + " " + working_sentence;
});

println(reversed.quoted());
}

// This code can produce the following output :
//
// "dog lazy the over jumps fox brown quick the"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(enumerable_aggregate2)
find_package(xtd REQUIRED)
add_sources(README.md src/enumerable_aggregate2.cpp)
target_type(CONSOLE_APPLICATION)
23 changes: 23 additions & 0 deletions examples/xtd.core.examples/linq/enumerable_aggregate2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# enumerable_aggregate2

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

## Sources

[src/enumerable_aggregate2.cpp](src/enumerable_aggregate2.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

```
The number of even integers is: 6
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <xtd/linq/enumerable>
#include <xtd/array>
#include <xtd/println>

using namespace xtd;
using namespace xtd::linq;

auto main() -> int {
auto ints = array {4, 8, 8, 3, 9, 0, 7, 8, 2};

// Count the even numbers in the array, using a seed value of 0.
auto num_even = enumerable::aggregate(ints, 0, [](int total, int next) {
return next % 2 == 0 ? total + 1 : total;
});

println("The number of even integers is: {}", num_even);
}

// This code can produce the following output :
//
// The number of even integers is: 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(enumerable_aggregate3)
find_package(xtd REQUIRED)
add_sources(README.md src/enumerable_aggregate3.cpp)
target_type(CONSOLE_APPLICATION)
23 changes: 23 additions & 0 deletions examples/xtd.core.examples/linq/enumerable_aggregate3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# enumerable_aggregate3

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

## Sources

[src/enumerable_aggregate3.cpp](src/enumerable_aggregate3.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

```
The fruit with the longest name is PASSIONFRUIT
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <xtd/linq/enumerable>
#include <xtd/array>
#include <xtd/println>

using namespace xtd;
using namespace xtd::linq;

auto main() -> int {
auto fruits = array {"apple"_s, "mango"_s, "orange"_s, "passionfruit"_s, "grape"_s};

// Determine whether any string in the array is longer than "banana".
auto longest_name =
enumerable::aggregate(fruits, "bananas"_s,
[](const string& longest, const string& next) {return next.length() > longest.length() ? next : longest;},
// Return the final result as an upper case string.
[](const string& fruit) {return fruit.to_upper();});

println("The fruit with the longest name is {}", longest_name);
}

// This code can produce the following output :
//
// The fruit with the longest name is PASSIONFRUIT
6 changes: 6 additions & 0 deletions examples/xtd.core.examples/linq/enumerable_all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(enumerable_all)
find_package(xtd REQUIRED)
add_sources(README.md src/enumerable_all.cpp)
target_type(CONSOLE_APPLICATION)
23 changes: 23 additions & 0 deletions examples/xtd.core.examples/linq/enumerable_all/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# enumerable_all

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

## Sources

[src/enumerable_all.cpp](src/enumerable_all.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

```
"dog lazy the over jumps fox brown quick the"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <xtd/linq/enumerable>
#include <xtd/println>

using namespace xtd;
using namespace xtd::linq;

auto main() -> int {
struct pet : public object {
pet() = default;
pet(const string& name, int age) : name {name}, age {age} {}

string name;
int age = 0;
};

// Create an array of pets.
auto pets = array {
pet {"Barley", 10},
pet {"Boots", 4},
pet {"Whiskers", 6}
};

// Determine whether all pet names in the array start with 'B'.
bool all_start_with_b = enumerable::all(pets, [](const pet& pet) {
return pet.name.starts_with("B");
});

println("{} pet names start with 'B'.", all_start_with_b ? "All" : "Not all");
}

// This code can produce the following output :
//
// Not all pet names start with 'B'.

0 comments on commit cfdb1a7

Please sign in to comment.