-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b41b88f
commit cfdb1a7
Showing
16 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ add_projects( | |
interfaces | ||
io | ||
keywords | ||
linq | ||
mains | ||
math | ||
media | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
6 changes: 6 additions & 0 deletions
6
examples/xtd.core.examples/linq/enumerable_aggregate/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
examples/xtd.core.examples/linq/enumerable_aggregate/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
23 changes: 23 additions & 0 deletions
23
examples/xtd.core.examples/linq/enumerable_aggregate/src/enumerable_aggregate.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
6 changes: 6 additions & 0 deletions
6
examples/xtd.core.examples/linq/enumerable_aggregate2/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
examples/xtd.core.examples/linq/enumerable_aggregate2/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
21 changes: 21 additions & 0 deletions
21
examples/xtd.core.examples/linq/enumerable_aggregate2/src/enumerable_aggregate2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
examples/xtd.core.examples/linq/enumerable_aggregate3/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
examples/xtd.core.examples/linq/enumerable_aggregate3/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
23 changes: 23 additions & 0 deletions
23
examples/xtd.core.examples/linq/enumerable_aggregate3/src/enumerable_aggregate3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
examples/xtd.core.examples/linq/enumerable_all/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
33 changes: 33 additions & 0 deletions
33
examples/xtd.core.examples/linq/enumerable_all/src/enumerable_all.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. |