xtd 0.2.0
Loading...
Searching...
No Matches
xtd::collections::generic::extensions::enumerable< enumerable_t, source_t > Class Template Reference
Inheritance diagram for xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >:
xtd::basic_array< value_type, allocator_t > xtd::basic_array< type_t, allocator_t > xtd::array< value_type >

Definition

template<typename enumerable_t, typename source_t>
class xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >

Provides a set of static methods for querying objects that implement ienumerable <type_t>.

Definition
template<typename type_t>
Provides a set of static methods for querying objects that implement ienumerable <type_t>.
Definition enumerable.hpp:31
Represent a static object. A static class can't be instantiated (constructors are deleted).
Definition static.hpp:20
Header
#include <xtd/linq/enumerable
Namespace
xtd::linq
Library
xtd.core

Public Static Methods

source_t aggregate (const std::function< source_t(const source_t &, const source_t &)> &func) const
 Applies an accumulator function over a sequence.
 
template<typename func_t >
source_t aggregate (const func_t &func) const
 Applies an accumulator function over a sequence.
 
template<typename accumulate_t >
accumulate_t aggregate (const accumulate_t &seed, const std::function< accumulate_t(const source_t &, const accumulate_t &)> &func) const
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
 
template<typename accumulate_t , typename func_t >
accumulate_t aggregate (const accumulate_t &seed, const func_t &func) const
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
 
template<typename accumulate_t , typename result_t >
result_t aggregate (const accumulate_t &seed, const std::function< accumulate_t(const source_t &, const accumulate_t &)> &func, const std::function< result_t(const accumulate_t &)> &result_selector) const
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
 
template<typename accumulate_t , typename result_t , typename func_t , typename result_selector_t >
result_t aggregate (const accumulate_t &seed, const func_t &func, const result_selector_t &result_selector) const
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
 
template<typename accumulate_t , typename func_t , typename result_selector_t >
accumulate_t aggregate (const accumulate_t &seed, const func_t &func, const result_selector_t &result_selector) const
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
 
bool all (const std::function< bool(const source_t &)> &predicate) const
 Determines whether all elements of a sequence satisfy a condition.
 
template<typename predicate_t >
bool all (const predicate_t &predicate) const
 Determines whether all elements of a sequence satisfy a condition.
 
bool any () const noexcept
 Determines whether a sequence contains any elements.
 
bool any (const std::function< bool(const source_t &)> &predicate) const
 Determines whether any element of a sequence satisfies a condition.
 
template<typename predicate_t >
bool any (const predicate_t &predicate) const
 Determines whether any elements of a sequence satisfy a condition.
 
xtd::linq::enumerable_collection< source_t > append (const source_t &element) const noexcept
 Appends a value to the end of the sequence.
 
xtd::linq::enumerable_collection< source_t > as_enumerable () const noexcept
 Returns the input typed as xtd::collection::generic::ienumerable <type_t>.
 

Member Function Documentation

◆ aggregate() [1/7]

template<typename enumerable_t , typename source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const std::function< source_t(const source_t &, const source_t &)> &  func) const
inline

Applies an accumulator function over a sequence.

Parameters
funcAn accumulator function to be invoked on each element.
Returns
The final accumulator value.
Examples
The following code example demonstrates how to reverse the order of words in a string by using enumerable::aggregate.
#include <xtd/console>
#include <xtd/string>
using namespace xtd;
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 = words.aggregate([](const string& working_sentence, const string& next) {
return next + " " + working_sentence;
});
console::write_line(reversed.quoted());
}
// This code produces the following output :
//
// "dog lazy the over jumps fox brown quick the"
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10

◆ aggregate() [2/7]

template<typename enumerable_t , typename source_t >
template<typename func_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const func_t &  func) const
inline

Applies an accumulator function over a sequence.

Parameters
funcAn accumulator function to be invoked on each element.
Returns
The final accumulator value.
Examples
The following code example demonstrates how to reverse the order of words in a string by using xtd::linq::enumerable::aggregate.
#include <xtd/console>
#include <xtd/string>
using namespace xtd;
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 = words.aggregate([](const string& working_sentence, const string& next) {
return next + " " + working_sentence;
});
console::write_line(reversed.quoted());
}
// This code produces the following output :
//
// "dog lazy the over jumps fox brown quick the"

◆ aggregate() [3/7]

template<typename enumerable_t , typename source_t >
template<typename accumulate_t >
accumulate_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const accumulate_t &  seed,
const std::function< accumulate_t(const source_t &, const accumulate_t &)> &  func 
) const
inline

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.

Parameters
seedThe initial accumulator value.
funcAn accumulator function to be invoked on each element.
Returns
The final accumulator value.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::aggregate to apply an accumulator function and use a seed value.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 = ints.aggregate(0, [](int total, int next) {
return next % 2 == 0 ? total + 1 : total;
});
console::write_line("The number of even integers is: {}", num_even);
}
// This code produces the following output :
//
// The number of even integers is: 6
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
source_t aggregate(const std::function< source_t(const source_t &, const source_t &)> &func) const
Applies an accumulator function over a sequence.
Definition enumerable.hpp:42

◆ aggregate() [4/7]

template<typename enumerable_t , typename source_t >
template<typename accumulate_t , typename func_t >
accumulate_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const accumulate_t &  seed,
const func_t &  func 
) const
inline

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.

Parameters
seedThe initial accumulator value.
funcAn accumulator function to be invoked on each element.
Returns
The final accumulator value.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::aggregate to apply an accumulator function and use a seed value.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 = ints.aggregate(0, [](int total, int next) {
return next % 2 == 0 ? total + 1 : total;
});
console::write_line("The number of even integers is: {}", num_even);
}
// This code produces the following output :
//
// The number of even integers is: 6

◆ aggregate() [5/7]

template<typename enumerable_t , typename source_t >
template<typename accumulate_t , typename result_t >
result_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const accumulate_t &  seed,
const std::function< accumulate_t(const source_t &, const accumulate_t &)> &  func,
const std::function< result_t(const accumulate_t &)> &  result_selector 
) const
inline

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

Parameters
seedThe initial accumulator value.
funcAn accumulator function to be invoked on each element.
result_SelectorA function to transform the final accumulator value into the result value.
Returns
The transformed final accumulator value.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::aggregate to apply an accumulator function and use a seed value.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 =
fruits.aggregate("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();});
console::write_line("The fruit with the longest name is {}", longest_name);
}
// This code produces the following output :
//
// The fruit with the longest name is PASSIONFRUIT
basic_string to_upper() const noexcept
Returns a copy of the current xtd::basic_string converted to uppercase.
Definition basic_string.hpp:1953
size_type length() const noexcept
Gets the number of characters in the current xtd::basic_string object.
Definition basic_string.hpp:918

◆ aggregate() [6/7]

template<typename enumerable_t , typename source_t >
template<typename accumulate_t , typename result_t , typename func_t , typename result_selector_t >
result_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const accumulate_t &  seed,
const func_t &  func,
const result_selector_t &  result_selector 
) const
inline

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

Parameters
seedThe initial accumulator value.
funcAn accumulator function to be invoked on each element.
result_selectorA function to transform the final accumulator value into the result value.
Returns
The transformed final accumulator value.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::aggregate to apply an accumulator function and use a seed value.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 =
fruits.aggregate("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();});
console::write_line("The fruit with the longest name is {}", longest_name);
}
// This code produces the following output :
//
// The fruit with the longest name is PASSIONFRUIT

◆ aggregate() [7/7]

template<typename enumerable_t , typename source_t >
template<typename accumulate_t , typename func_t , typename result_selector_t >
accumulate_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const accumulate_t &  seed,
const func_t &  func,
const result_selector_t &  result_selector 
) const
inline

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

Parameters
seedThe initial accumulator value.
funcAn accumulator function to be invoked on each element.
result_SelectorA function to transform the final accumulator value into the result value.
Returns
The transformed final accumulator value.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::aggregate to apply an accumulator function and use a seed value.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 =
fruits.aggregate("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();});
console::write_line("The fruit with the longest name is {}", longest_name);
}
// This code produces the following output :
//
// The fruit with the longest name is PASSIONFRUIT

◆ all() [1/2]

template<typename enumerable_t , typename source_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::all ( const std::function< bool(const source_t &)> &  predicate) const
inline

Determines whether all elements of a sequence satisfy a condition.

Parameters
predicateA function to test each element for a condition.
Returns
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::all <source_t> to determine whether all the elements in a sequence satisfy a condition. Variable all_start_with_B is true if all the pet names start with "B" or if the pets array is empty.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 = pets.all([](const pet& pet) {
return pet.name.starts_with("B");
});
console::write_line("{} pet names start with 'B'.", all_start_with_b ? "All" : "Not all");
}
// This code produces the following output :
//
// Not all pet names start with 'B'.
bool all(const std::function< bool(const source_t &)> &predicate) const
Determines whether all elements of a sequence satisfy a condition.
Definition enumerable.hpp:120
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:42

◆ all() [2/2]

template<typename enumerable_t , typename source_t >
template<typename predicate_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::all ( const predicate_t &  predicate) const
inline

Determines whether all elements of a sequence satisfy a condition.

Parameters
predicateA function to test each element for a condition.
Returns
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::all <source_t> to determine whether all the elements in a sequence satisfy a condition. Variable all_start_with_B is true if all the pet names start with "B" or if the pets array is empty.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 = pets.all([](const pet& pet) {
return pet.name.starts_with("B");
});
console::write_line("{} pet names start with 'B'.", all_start_with_b ? "All" : "Not all");
}
// This code produces the following output :
//
// Not all pet names start with 'B'.

◆ any() [1/3]

template<typename enumerable_t , typename source_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::any ( ) const
inlinenoexcept

Determines whether a sequence contains any elements.

Returns
true if the source sequence contains any elements; otherwise, false.
Examples
The following code example demonstrates how to use Any to determine whether a sequence contains any elements.

◆ any() [2/3]

template<typename enumerable_t , typename source_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::any ( const std::function< bool(const source_t &)> &  predicate) const
inline

Determines whether any element of a sequence satisfies a condition.

Parameters
predicateA function to test each element for a condition.
Returns
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::all <source_t> to determine whether all the elements in a sequence satisfy a condition. Variable all_start_with_B is true if all the pet names start with "B" or if the pets array is empty.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 = pets.all([](const pet& pet) {
return pet.name.starts_with("B");
});
console::write_line("{} pet names start with 'B'.", all_start_with_b ? "All" : "Not all");
}
// This code produces the following output :
//
// Not all pet names start with 'B'.

◆ any() [3/3]

template<typename enumerable_t , typename source_t >
template<typename predicate_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::any ( const predicate_t &  predicate) const
inline

Determines whether any elements of a sequence satisfy a condition.

Parameters
predicateA function to test each element for a condition.
Returns
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::all <source_t> to determine whether all the elements in a sequence satisfy a condition. Variable all_start_with_B is true if all the pet names start with "B" or if the pets array is empty.
#include <xtd/array>
#include <xtd/console>
using namespace xtd;
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 = pets.all([](const pet& pet) {
return pet.name.starts_with("B");
});
console::write_line("{} pet names start with 'B'.", all_start_with_b ? "All" : "Not all");
}
// This code produces the following output :
//
// Not all pet names start with 'B'.

◆ append()

template<typename enumerable_t , typename source_t >
xtd::linq::enumerable_collection< source_t > xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::append ( const source_t &  element) const
inlinenoexcept

Appends a value to the end of the sequence.

Parameters
elementThe value to append to source.
Returns
A new sequence that ends with element.
Examples
The following code example demonstrates how to use Append to append a value to the end of the sequence.
#include <xtd/collections/generic/list>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
// Creating a list of numbers
auto numbers = list {1, 2, 3, 4};
// Trying to append any value of the same type
numbers.append(5);
// It doesn't work because the original list has not been changed
// It works now because we are using a changed copy of the original list
console::write_line(string::join(", ", numbers.append(5)));
// If you prefer, you can create a new list explicitly
auto new_numbers = numbers.append(5).to_list();
// And then write to the console output
console::write_line(string::join(", ", new_numbers));
}
// This code produces the following output :
//
// 1, 2, 3, 4
// 1, 2, 3, 4, 5
// 1, 2, 3, 4, 5
static basic_string join(const basic_string separator, const collection_t &values) noexcept
Concatenates a specified separator basic_string between each element of a specified object array,...
Definition basic_string.hpp:2296
xtd::linq::enumerable_collection< source_t > append(const source_t &element) const noexcept
Appends a value to the end of the sequence.
Definition enumerable.hpp:168
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:72
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15

◆ as_enumerable()

template<typename enumerable_t , typename source_t >
xtd::linq::enumerable_collection< source_t > xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::as_enumerable ( ) const
inlinenoexcept

Returns the input typed as xtd::collection::generic::ienumerable <type_t>.

Parameters
sourceA sequence of values.
Returns
The input sequence typed as xtd::collection::generic::ienumerable <type_t>.
Example
The following code example demonstrates how to use as_enumerable <source_t>(ienumerable <source_t>) to hide a type's custom Where method when the standard query operator implementation is desired.

The documentation for this class was generated from the following file: