From cfdb1a7516e13b2309eeeb3becd80ee4422f4c9f Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Fri, 20 Dec 2024 22:19:01 +0100 Subject: [PATCH] Add xtd::linq::enumerable examples --- examples/xtd.core.examples/CMakeLists.txt | 1 + examples/xtd.core.examples/README.md | 7 ++++ .../xtd.core.examples/linq/CMakeLists.txt | 11 +++++++ examples/xtd.core.examples/linq/README.md | 16 +++++++++ .../linq/enumerable_aggregate/CMakeLists.txt | 6 ++++ .../linq/enumerable_aggregate/README.md | 23 +++++++++++++ .../src/enumerable_aggregate.cpp | 23 +++++++++++++ .../linq/enumerable_aggregate2/CMakeLists.txt | 6 ++++ .../linq/enumerable_aggregate2/README.md | 23 +++++++++++++ .../src/enumerable_aggregate2.cpp | 21 ++++++++++++ .../linq/enumerable_aggregate3/CMakeLists.txt | 6 ++++ .../linq/enumerable_aggregate3/README.md | 23 +++++++++++++ .../src/enumerable_aggregate3.cpp | 23 +++++++++++++ .../linq/enumerable_all/CMakeLists.txt | 6 ++++ .../linq/enumerable_all/README.md | 23 +++++++++++++ .../enumerable_all/src/enumerable_all.cpp | 33 +++++++++++++++++++ 16 files changed, 251 insertions(+) create mode 100644 examples/xtd.core.examples/linq/CMakeLists.txt create mode 100644 examples/xtd.core.examples/linq/README.md create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate/CMakeLists.txt create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate/README.md create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate/src/enumerable_aggregate.cpp create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate2/CMakeLists.txt create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate2/README.md create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate2/src/enumerable_aggregate2.cpp create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate3/CMakeLists.txt create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate3/README.md create mode 100644 examples/xtd.core.examples/linq/enumerable_aggregate3/src/enumerable_aggregate3.cpp create mode 100644 examples/xtd.core.examples/linq/enumerable_all/CMakeLists.txt create mode 100644 examples/xtd.core.examples/linq/enumerable_all/README.md create mode 100644 examples/xtd.core.examples/linq/enumerable_all/src/enumerable_all.cpp diff --git a/examples/xtd.core.examples/CMakeLists.txt b/examples/xtd.core.examples/CMakeLists.txt index 8399c78de53a..e3d582c29ab9 100644 --- a/examples/xtd.core.examples/CMakeLists.txt +++ b/examples/xtd.core.examples/CMakeLists.txt @@ -25,6 +25,7 @@ add_projects( interfaces io keywords + linq mains math media diff --git a/examples/xtd.core.examples/README.md b/examples/xtd.core.examples/README.md index e8edb790bb13..9a2dac225295 100644 --- a/examples/xtd.core.examples/README.md +++ b/examples/xtd.core.examples/README.md @@ -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. diff --git a/examples/xtd.core.examples/linq/CMakeLists.txt b/examples/xtd.core.examples/linq/CMakeLists.txt new file mode 100644 index 000000000000..60ebb4700aa7 --- /dev/null +++ b/examples/xtd.core.examples/linq/CMakeLists.txt @@ -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 +) diff --git a/examples/xtd.core.examples/linq/README.md b/examples/xtd.core.examples/linq/README.md new file mode 100644 index 000000000000..a49a25563151 --- /dev/null +++ b/examples/xtd.core.examples/linq/README.md @@ -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 +``` diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate/CMakeLists.txt b/examples/xtd.core.examples/linq/enumerable_aggregate/CMakeLists.txt new file mode 100644 index 000000000000..6dcd8d78e081 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate/CMakeLists.txt @@ -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) diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate/README.md b/examples/xtd.core.examples/linq/enumerable_aggregate/README.md new file mode 100644 index 000000000000..9b042a944bf3 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate/README.md @@ -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" +``` diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate/src/enumerable_aggregate.cpp b/examples/xtd.core.examples/linq/enumerable_aggregate/src/enumerable_aggregate.cpp new file mode 100644 index 000000000000..519dd00e020a --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate/src/enumerable_aggregate.cpp @@ -0,0 +1,23 @@ +#include +#include + +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" diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate2/CMakeLists.txt b/examples/xtd.core.examples/linq/enumerable_aggregate2/CMakeLists.txt new file mode 100644 index 000000000000..3b401d3bdf5e --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate2/CMakeLists.txt @@ -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) diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate2/README.md b/examples/xtd.core.examples/linq/enumerable_aggregate2/README.md new file mode 100644 index 000000000000..ac9b2ed0c61d --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate2/README.md @@ -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 +``` diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate2/src/enumerable_aggregate2.cpp b/examples/xtd.core.examples/linq/enumerable_aggregate2/src/enumerable_aggregate2.cpp new file mode 100644 index 000000000000..1d1da4550227 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate2/src/enumerable_aggregate2.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +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 diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate3/CMakeLists.txt b/examples/xtd.core.examples/linq/enumerable_aggregate3/CMakeLists.txt new file mode 100644 index 000000000000..61c8aaef81e5 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate3/CMakeLists.txt @@ -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) diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate3/README.md b/examples/xtd.core.examples/linq/enumerable_aggregate3/README.md new file mode 100644 index 000000000000..d35454b43055 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate3/README.md @@ -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 +``` diff --git a/examples/xtd.core.examples/linq/enumerable_aggregate3/src/enumerable_aggregate3.cpp b/examples/xtd.core.examples/linq/enumerable_aggregate3/src/enumerable_aggregate3.cpp new file mode 100644 index 000000000000..f8cd178c44a9 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_aggregate3/src/enumerable_aggregate3.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +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 diff --git a/examples/xtd.core.examples/linq/enumerable_all/CMakeLists.txt b/examples/xtd.core.examples/linq/enumerable_all/CMakeLists.txt new file mode 100644 index 000000000000..929f575e36de --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_all/CMakeLists.txt @@ -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) diff --git a/examples/xtd.core.examples/linq/enumerable_all/README.md b/examples/xtd.core.examples/linq/enumerable_all/README.md new file mode 100644 index 000000000000..4f37932f4a60 --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_all/README.md @@ -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" +``` diff --git a/examples/xtd.core.examples/linq/enumerable_all/src/enumerable_all.cpp b/examples/xtd.core.examples/linq/enumerable_all/src/enumerable_all.cpp new file mode 100644 index 000000000000..4beb7e335f4b --- /dev/null +++ b/examples/xtd.core.examples/linq/enumerable_all/src/enumerable_all.cpp @@ -0,0 +1,33 @@ +#include +#include + +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'.