xtd 0.2.0
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::collections::generic::ienumerable< char_t > xtd::collections::generic::ienumerable< bool > xtd::collections::generic::ienumerable< xtd::collections::generic::key_value_pair< key_t, value_t > > xtd::collections::generic::ienumerable< list_type_t > xtd::collections::generic::ienumerable< xtd::sptr< xtd::diagnostics::trace_listener > > xtd::collections::generic::ienumerable< value_type > xtd::collections::generic::ienumerable< int32 > xtd::collections::generic::ienumerable< xtd::string > xtd::collections::generic::ienumerable< xtd::drawing::color > xtd::collections::generic::ienumerable< xtd::byte > xtd::collections::generic::ienumerable< xtd::drawing::imaging::encoder_parameter > xtd::collections::generic::ienumerable< xtd::basic_string > xtd::collections::generic::ienumerable< xtd::shared_ptr_object< xtd::forms::menu_item > > xtd::collections::generic::ienumerable< xtd::forms::style_sheets::shadow > xtd::collections::generic::ienumerable< byte > xtd::collections::generic::ienumerable< xtd::uint16 > xtd::collections::generic::ienumerable< xtd::net::ip_address > xtd::collections::generic::ienumerable< xtd::array< xtd::byte > > xtd::collections::generic::ienumerable< xtd::time_zone_info::adjustement_rule > xtd::collections::generic::ienumerable< xtd::tunit::test >

Definition

template<class enumerable_t, class 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<class type_t>
Provides a set of static methods for querying objects that implement ienumerable <type_t>.
Definition enumerable.hpp:32
Represent a static object. A static class can't be instantiated (constructors are deleted).
Definition static.hpp:20
Header
#include <xtd/linq/generic/extensions/enumerable>
Namespace
xtd::linq
Library
xtd.core

Public Aliases

using enumerable_type = enumerable_t
 Represents the ienumerable enumerable type.
 
using source_type = source_t
 Represents the ienumerable source type.
 
template<class type_t >
using ienumerable = typename xtd::linq::enumerable::ienumerable< type_t >
 Represents the ienumerable value type.
 
template<class type_t >
using list = typename xtd::linq::enumerable::list< type_t >
 Represents the list value type.
 

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.
 
source_t aggregate (const source_t &seed, const std::function< source_t(const source_t &, const source_t &)> &func) const
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
 
template<class 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.
 
source_t aggregate (const source_t &seed, const std::function< source_t(const source_t &, const source_t &)> &func, const std::function< source_t(const source_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<class result_t , class accumulate_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.
 
bool all (const std::function< bool(const source_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.
 
const ienumerable< source_t > & append (const source_t &element) const noexcept
 Appends a value to the end of the sequence.
 
const ienumerable< source_t > & as_enumerable () const noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
auto average () const noexcept
 Computes the average of a sequence of source_t values.
 
template<class result_t >
const ienumerable< result_t > & cast () const noexcept
 Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
 
const ienumerable< xtd::array< source_t > > & chunk (size_t size) const
 Splits the elements of a sequence into chunks of size at most size.
 
const ienumerable< source_t > & concat (const ienumerable< source_t > &second) const noexcept
 Concatenates two sequences.
 
bool contains (const source_t &value) const noexcept
 Determines whether a sequence contains a specified element by using the default equality comparer.
 
bool contains (const source_t &value, const xtd::collections::generic::iequality_comparer< source_t > &comparer) const noexcept
 Determines whether a sequence contains a specified element by using a specified equality comparer.
 
size_t count () const noexcept
 Returns the number of elements in current sequence.
 
size_t count (const std::function< bool(const source_t &)> &predicate) const noexcept
 Returns a number that represents how many elements in the specified sequence satisfy a condition.
 
xtd::size count (const source_t &value) const noexcept
 Returns the number of elements with the specified value.
 
template<class key_t >
const ienumerable< key_value_pair< key_t, xtd::size > > & count_by (const std::function< key_t(const source_t &)> &key_selector) const noexcept
 Returns the count of elements in the current sequence grouped by key.
 
template<class key_t >
const ienumerable< key_value_pair< key_t, xtd::size > > & count_by (const std::function< key_t(const source_t &)> &key_selector, const iequality_comparer< key_t > &key_comparer) const noexcept
 Returns the count of elements in the current sequence grouped by key.
 
const ienumerable< source_t > & default_if_empty () const noexcept
 Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the current sequence is empty.
 
const ienumerable< source_t > & default_if_empty (const source_t &default_value) const noexcept
 Returns the elements of the specified sequence or the specified value in a singleton collection if the current sequence is empty.
 
source_t first_or_default (const std::function< bool(const source_t &)> &predicate, const source_t &default_value) const noexcept
 Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
 
source_t first_or_default (const std::function< bool(const source_t &)> &predicate) const noexcept
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
 
source_t first_or_default (const source_t default_value) const noexcept
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
 
source_t first_or_default () const noexcept
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
 
template<class result_t >
const ienumerable< result_t > & select (const std::function< result_t(const source_t &)> &selector) const
 Projects each element of a sequence into a new form.
 
const ienumerable< source_t > & select (const std::function< source_t(const source_t &)> &selector) const
 Projects each element of a sequence into a new form.
 
template<class result_t >
const ienumerable< result_t > & select (const std::function< result_t(const source_t &, size_t index)> &selector) const
 Projects each element of a sequence into a new form by incorporating the element's index.
 
const ienumerable< source_t > & select (const std::function< source_t(const source_t &, size_t index)> &selector) const
 Projects each element of a sequence into a new form by incorporating the element's index.
 
const list< source_t > & to_list () const noexcept
 Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
 
const ienumerable< source_t > & where (const std::function< bool(const source_t &)> &predicate) const
 Filters a sequence of values based on a predicate.
 
const ienumerable< source_t > & where (const std::function< bool(const source_t &, size_t)> &predicate) const
 Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
 

Member Typedef Documentation

◆ enumerable_type

template<class enumerable_t , class source_t >
using xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::enumerable_type = enumerable_t

Represents the ienumerable enumerable type.

◆ source_type

template<class enumerable_t , class source_t >
using xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::source_type = source_t

Represents the ienumerable source type.

◆ ienumerable

template<class enumerable_t , class source_t >
template<class type_t >
using xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::ienumerable = typename xtd::linq::enumerable::ienumerable<type_t>

Represents the ienumerable value type.

◆ list

template<class enumerable_t , class source_t >
template<class type_t >
using xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::list = typename xtd::linq::enumerable::list<type_t>

Represents the list value type.

Member Function Documentation

◆ aggregate() [1/5]

template<class enumerable_t , class 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/5]

template<class enumerable_t , class source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const source_t &  seed,
const std::function< source_t(const source_t &, const source_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:61
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:59

◆ aggregate() [3/5]

template<class enumerable_t , class source_t >
template<class 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.

Template Parameters
accumulate_tThe type of the 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() [4/5]

template<class enumerable_t , class source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::aggregate ( const source_t &  seed,
const std::function< source_t(const source_t &, const source_t &)> &  func,
const std::function< source_t(const source_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<string> {"apple", "mango", "orange", "passionfruit", "grape"};
// Determine whether any string in the array is longer than "banana".
auto longest_name = fruits.aggregate("bananas",
[](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:1945
size_type length() const noexcept
Gets the number of characters in the current xtd::basic_string object.
Definition basic_string.hpp:910

◆ aggregate() [5/5]

template<class enumerable_t , class source_t >
template<class result_t , class accumulate_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.

Template Parameters
result_tThe type of the resulting value.
accumulate_tThe type of the accumulator 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<string> {"apple", "mango", "orange", "passionfruit", "grape"};
// Determine whether any string in the array is longer than "banana".
auto longest_name = fruits.aggregate("bananas",
[](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()

template<class enumerable_t , class 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:116
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:43

◆ any() [1/2]

template<class enumerable_t , class 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.
#include <xtd/collections/generic/list>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
auto numbers = list {1, 2};
auto has_elements = numbers.any();
console::write_line("The list {0} empty.", has_elements ? "is not" : "is");
}
// This code produces the following output :
//
// The list is not empty.
typename xtd::linq::enumerable::list< type_t > list
Represents the list value type.
Definition enumerable.hpp:47
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16

◆ any() [2/2]

template<class enumerable_t , class 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/collections/generic/list>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
auto numbers = list {1, 2};
auto has_elements = numbers.any();
console::write_line("The list {0} empty.", has_elements ? "is not" : "is");
}
// This code produces the following output :
//
// The list is not empty.

◆ append()

template<class enumerable_t , class source_t >
const ienumerable< 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:2288

◆ as_enumerable()

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::as_enumerable ( ) const
inlinenoexcept

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

Returns
The input sequence typed as xtd::collections::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.

◆ average()

template<class enumerable_t , class source_t >
auto xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::average ( ) const
inlinenoexcept

Computes the average of a sequence of source_t values.

Returns
The average of this sequence of values.
Exceptions
xtd::invalid_operation_exceptionthis sequence contains no elements.

◆ cast()

template<class enumerable_t , class source_t >
template<class result_t >
const ienumerable< result_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::cast ( ) const
inlinenoexcept

Casts the elements of an xtd::collections::generic::ienumerable to the specified type.

Template Parameters
result_tThe type of the resulting value.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains each element of the source sequence cast to the specified type.
Exceptions
xtd::invalid_cast_exceptionAn element in the sequence cannot be cast to type `result_t`.

◆ chunk()

template<class enumerable_t , class source_t >
const ienumerable< xtd::array< source_t > > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::chunk ( size_t  size) const
inline

Splits the elements of a sequence into chunks of size at most size.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to chunk.
sizeThe maximum size of each chunk.
Returns
A sequence of chunks of size at most size.

◆ concat()

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::concat ( const ienumerable< source_t > &  second) const
inlinenoexcept

Concatenates two sequences.

Parameters
secondThe sequence to concatenate to the current sequence.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains the concatenated elements of the two input sequences.

◆ contains() [1/2]

template<class enumerable_t , class source_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::contains ( const source_t &  value) const
inlinenoexcept

Determines whether a sequence contains a specified element by using the default equality comparer.

Parameters
valueThe value to locate in the sequence.
Returns
true if the source sequence contains an element that has the specified value; otherwise, false.

◆ contains() [2/2]

template<class enumerable_t , class source_t >
bool xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::contains ( const source_t &  value,
const xtd::collections::generic::iequality_comparer< source_t > &  comparer 
) const
inlinenoexcept

Determines whether a sequence contains a specified element by using a specified equality comparer.

Parameters
valueThe value to locate in the sequence.
comparerAn equality comparer to compare values.
Returns
true if the source sequence contains an element that has the specified value; otherwise, false.

◆ count() [1/3]

template<class enumerable_t , class source_t >
size_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::count ( ) const
inlinenoexcept

Returns the number of elements in current sequence.

Returns
The number of elements in the input sequence.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::count <source_t>(const ienumerable <source_t>&) to count the elements in a sequence.
#include <xtd/xtd>
using namespace xtd;
auto main() -> int {
auto fruits = array {"apple", "banana", "mango", "orange", "passionfruit", "grape"};
auto number_of_fruits = fruits.enumerable::count();
console::write_line("There are {0} fruits in the collection.", number_of_fruits);
}
// This code produces the following output :
//
// There are 6 fruits in the collection.

◆ count() [2/3]

template<class enumerable_t , class source_t >
size_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::count ( const std::function< bool(const source_t &)> &  predicate) const
inlinenoexcept

Returns a number that represents how many elements in the specified sequence satisfy a condition.

Parameters
predicateA function to test each element for a condition.
Returns
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::count <source_t>(const ienumerable <source_t>&, const std::function <bool(const source_t&)>&) to count the elements in a sequence that satisfy a condition.
#include <xtd/xtd>
using namespace xtd;
struct pet {
string name;
bool vaccinated = false;
friend bool operator ==(const pet& lhs, const pet& rhs) noexcept {
return lhs.name == rhs.name && lhs.vaccinated == rhs.vaccinated;
}
};
auto main() -> int {
auto pets = array {
pet {.name = "Barley", .vaccinated = true},
pet {.name = "Boots", .vaccinated = false},
pet {.name = "Whiskers", .vaccinated = false}
};
auto number_unvaccinated = pets.count([](const pet& pet) {return !pet.vaccinated;});
console::write_line("There are {} unvaccinated animals.", number_unvaccinated);
}
// This code produces the following output :
//
// There are 2 unvaccinated animals.
size_type count() const noexcept override
Gets the number of elements contained in the xtd::array <type_t>.
Definition basic_array.hpp:134

◆ count() [3/3]

template<class enumerable_t , class source_t >
xtd::size xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::count ( const source_t &  value) const
inlinenoexcept

Returns the number of elements with the specified value.

Parameters
valueThe value to search for.
Returns
A number representing the number of elements in the sequence that are equal to the value.

◆ count_by() [1/2]

template<class enumerable_t , class source_t >
template<class key_t >
const ienumerable< key_value_pair< key_t, xtd::size > > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::count_by ( const std::function< key_t(const source_t &)> &  key_selector) const
inlinenoexcept

Returns the count of elements in the current sequence grouped by key.

Template Parameters
key_tThe type of the key returned by key_selector.
Parameters
key_selectorA function to extract the key for each element.
Returns
An enumerable containing the frequencies of each key occurrence in current sequence.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::count_by <source_t>(const ienumerable <source_t>&, const std::function <key_t(const source_t&)>&) to count the number of elements in a sequence grouped by key.
#include <xtd/xtd>
using namespace xtd;
struct student : iequatable<student> {
student() = default;
student(const string& name, const string& score) : name {name}, score {score} {}
string name;
string score;
bool equals(const student& other) const noexcept override {
return name == other.name && score == other.score;
}
};
auto main() -> int {
auto students = array<student> {
{"Alice", "A"},
{"Bob", "B"},
{"Charlie", "C"},
{"David", "B"},
{"Eve", "A"}
};
const auto& query = students.count_by<string>([](const auto& student) {return student.score;});
for (const auto& [score, count] : query)
console::write_line("Students with a {}-score: {}", score, count);
}
// This code produces the following output :
//
// Students with a A-score: 2
// Students with a B-score: 2
// Students with a C-score: 1
size_t count() const noexcept
Returns the number of elements in current sequence.
Definition enumerable.hpp:209
Represents the standard input, output, and error streams for console applications.
Definition console.hpp:36
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
@ query
The xtd::uri::query data.
@ other
The operating system is other.

◆ count_by() [2/2]

template<class enumerable_t , class source_t >
template<class key_t >
const ienumerable< key_value_pair< key_t, xtd::size > > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::count_by ( const std::function< key_t(const source_t &)> &  key_selector,
const iequality_comparer< key_t > &  key_comparer 
) const
inlinenoexcept

Returns the count of elements in the current sequence grouped by key.

Template Parameters
key_tThe type of the key returned by key_selector.
Parameters
key_selectorA function to extract the key for each element.
key_comparerAn equality comparer to compare keys.
Returns
An enumerable containing the frequencies of each key occurrence in current sequence.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::count_by <source_t>(const ienumerable <source_t>&, const std::function <key_t(const source_t&)>&) to count the number of elements in a sequence grouped by key.
#include <xtd/xtd>
using namespace xtd;
struct student : iequatable<student> {
student() = default;
student(const string& name, const string& score) : name {name}, score {score} {}
string name;
string score;
bool equals(const student& other) const noexcept override {
return name == other.name && score == other.score;
}
};
auto main() -> int {
auto students = array<student> {
{"Alice", "A"},
{"Bob", "B"},
{"Charlie", "C"},
{"David", "B"},
{"Eve", "A"}
};
const auto& query = students.count_by<string>([](const auto& student) {return student.score;});
for (const auto& [score, count] : query)
console::write_line("Students with a {}-score: {}", score, count);
}
// This code produces the following output :
//
// Students with a A-score: 2
// Students with a B-score: 2
// Students with a C-score: 1

◆ default_if_empty() [1/2]

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::default_if_empty ( ) const
inlinenoexcept

Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the current sequence is empty.

Returns
An xtd::collections::generic::ienumerable <type_t> that contains default_value if source is empty; otherwise, source.

◆ default_if_empty() [2/2]

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::default_if_empty ( const source_t &  default_value) const
inlinenoexcept

Returns the elements of the specified sequence or the specified value in a singleton collection if the current sequence is empty.

Parameters
default_valueThe value to return if the sequence is empty.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains default_value if source is empty; otherwise, source.

◆ first_or_default() [1/4]

template<class enumerable_t , class source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::first_or_default ( const std::function< bool(const source_t &)> &  predicate,
const source_t &  default_value 
) const
inlinenoexcept

Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.

Parameters
predicateA function to test each element for a condition.
default_valueThe default value to return if the sequence is empty.
Returns
default_value if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.

◆ first_or_default() [2/4]

template<class enumerable_t , class source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::first_or_default ( const std::function< bool(const source_t &)> &  predicate) const
inlinenoexcept

Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.

Parameters
predicateA function to test each element for a condition.
Returns
default source_t {} if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.

◆ first_or_default() [3/4]

template<class enumerable_t , class source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::first_or_default ( const source_t  default_value) const
inlinenoexcept

Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.

Parameters
default_valueThe default value to return if the sequence is empty.
Returns
default_value if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.

◆ first_or_default() [4/4]

template<class enumerable_t , class source_t >
source_t xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::first_or_default ( ) const
inlinenoexcept

Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.

Returns
default source_t {} if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.

◆ select() [1/4]

template<class enumerable_t , class source_t >
template<class result_t >
const ienumerable< result_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::select ( const std::function< result_t(const source_t &)> &  selector) const
inline

Projects each element of a sequence into a new form.

Template Parameters
result_tThe type of the resulting value.
Parameters
sourceA sequence of values to invoke a transform function on.
selectorA transform function to apply to each element.
Returns
An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::select <source_t, result_t>(const ienumerable <source_t>&, const std::function <result_t(const source_t&)>&) to project over a sequence of values.

◆ select() [2/4]

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::select ( const std::function< source_t(const source_t &)> &  selector) const
inline

Projects each element of a sequence into a new form.

Parameters
sourceA sequence of values to invoke a transform function on.
selectorA transform function to apply to each element.
Returns
An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::select <source_t, result_t>(const ienumerable <source_t>&, const std::function <result_t(const source_t&)>&) to project over a sequence of values.

◆ select() [3/4]

template<class enumerable_t , class source_t >
template<class result_t >
const ienumerable< result_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::select ( const std::function< result_t(const source_t &, size_t index)> &  selector) const
inline

Projects each element of a sequence into a new form by incorporating the element's index.

Template Parameters
result_tThe type of the resulting value.
Parameters
sourceA sequence of values to invoke a transform function on.
selectorA transform function to apply to each source element; the second parameter of the function represents the index of the source element.
Returns
An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::select <source_t, result_t>(const ienumerable <source_t>&, const std::function <result_t(const source_t&, size_t)>&) to project over a sequence of values and use the index of each element.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
auto fruits = array<string> {"apple", "banana", "mango", "orange", "passionfruit", "grape"};
const auto& query = fruits.select<std::tuple<size_t, string>>([](const string& fruit, size_t index) {
return std::make_tuple(index, fruit.substring(0, index));
});
for (const auto& [index, str] : query)
console::write_line("{{ index = {}, str = {} }}", index, str);
}
// This code produces the following output :
//
// { index = 0, str = }
// { index = 1, str = b }
// { index = 2, str = ma }
// { index = 3, str = ora }
// { index = 4, str = pass }
// { index = 5, str = grape }

◆ select() [4/4]

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::select ( const std::function< source_t(const source_t &, size_t index)> &  selector) const
inline

Projects each element of a sequence into a new form by incorporating the element's index.

Parameters
sourceA sequence of values to invoke a transform function on.
selectorA transform function to apply to each source element; the second parameter of the function represents the index of the source element.
Returns
An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::select <source_t, result_t>(const ienumerable <source_t>&, const std::function <result_t(const source_t&, size_t)>&) to project over a sequence of values and use the index of each element.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
auto fruits = array<string> {"apple", "banana", "mango", "orange", "passionfruit", "grape"};
const auto& query = fruits.select<std::tuple<size_t, string>>([](const string& fruit, size_t index) {
return std::make_tuple(index, fruit.substring(0, index));
});
for (const auto& [index, str] : query)
console::write_line("{{ index = {}, str = {} }}", index, str);
}
// This code produces the following output :
//
// { index = 0, str = }
// { index = 1, str = b }
// { index = 2, str = ma }
// { index = 3, str = ora }
// { index = 4, str = pass }
// { index = 5, str = grape }

◆ to_list()

template<class enumerable_t , class source_t >
const list< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::to_list ( ) const
noexcept

Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.

Returns
A xtd::collections::generic::list <type_t> that contains elements from the input sequence.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::to_list to force immediate query evaluation and return a xtd::collections::generic::list <type_t> that contains the query results.
#include <xtd/collections/generic/list>
#include <xtd/console>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
array<string> fruits = {"apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry"};
list<size_t> lengths = fruits.select<size_t>([](const string& fruit) {return fruit.length();}).to_list();
for (auto length : lengths)
console::write_line(length);
}
// This code produces the following output :
//
// 5
// 12
// 6
// 5
// 6
// 9
// 5
// 10
const ienumerable< result_t > & select(const std::function< result_t(const source_t &)> &selector) const
Projects each element of a sequence into a new form.
Definition enumerable.hpp:304
const list< source_t > & to_list() const noexcept
Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <ty...
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80

◆ where() [1/2]

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

Filters a sequence of values based on a predicate.

Parameters
predicateA function to test each element for a condition.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains elements from the input sequence that satisfy the condition.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::where <source_t>(const ienumerable <source_t>&, const std::function<bool (const source_t&)>&) to filter a sequence.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
array<string> fruits = {"apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry"};
const ienumerable<string>& query = fruits.where([](const string& fruit) {return fruit.length() < 6;});
for (const auto& fruit : query)
console::write_line(fruit);
}
// This code produces the following output :
//
// apple
// mango
// grape
const ienumerable< source_t > & where(const std::function< bool(const source_t &)> &predicate) const
Filters a sequence of values based on a predicate.
Definition enumerable.hpp:353
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:36

◆ where() [2/2]

template<class enumerable_t , class source_t >
const ienumerable< source_t > & xtd::collections::generic::extensions::enumerable< enumerable_t, source_t >::where ( const std::function< bool(const source_t &, size_t)> &  predicate) const
inline

Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.

Parameters
predicateA function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains elements from the input sequence that satisfy the condition.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::where <source_t>(const ienumerable <source_t>&, const std::function<bool (const source_t&, size_t)>&) to filter a sequence based on a predicate that involves the index of each element.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::collections::generic;
auto main() -> int {
array<int> numbers = {0, 30, 20, 15, 90, 85, 40, 75};
const ienumerable<int>& query = numbers.where([](int number, size_t index) {
return number <= int(index * 10);
});
for (const auto& number : query)
console::write_line(number);
}
// This code produces the following output :
//
// 0
// 20
// 15
// 40
@ number
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_trailing_sign...

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