xtd 1.0.0
Loading...
Searching...
No Matches
xtd::linq::enumerable Class Reference
Inheritance diagram for xtd::linq::enumerable:
xtd::static_object

Definition

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:73
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 Aliases

template<typename type_t>
using enumerator
 Represents the enumerator value type.
template<typename type_t>
using iequality_comparer
 Represents the ienumerable value type.
template<typename type_t>
using ienumerable
 Represents the ienumerable value type.
template<typename type_t>
using list
 Represents the list value type.
template<typename key_t, typename value_t>
using key_value_pair
 Represents the key value pair value type.

Public Static Methods

template<xtd::iterable source_t, xtd::func_callable< xtd::iterable_value_type< source_t >, xtd::iterable_value_type< source_t >, xtd::iterable_value_type< source_t > > func_t>
static auto aggregate (const source_t &source, func_t &&funcfunc) -> xtd::iterable_value_type< source_t >
 Applies an accumulator function over a sequence.
template<typename accumulate_t, xtd::iterable source_t, xtd::func_callable< accumulate_t, accumulate_t, xtd::iterable_value_type< source_t > > func_t>
static auto aggregate (const source_t &source, accumulate_t &&seed, func_t &&funcfunc) -> accumulate_t
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
template<typename result_t, typename accumulate_t, xtd::iterable source_t, xtd::func_callable< accumulate_t, accumulate_t, xtd::iterable_value_type< source_t > > func_t, xtd::func_callable< result_t, accumulate_t > result_selector_t>
static auto aggregate (const source_t &source, accumulate_t &&seed, func_t &&funcfunc, result_selector_t &&result_selector) -> result_t
 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<xtd::iterable source_t, xtd::predicate_callable< xtd::iterable_value_type< source_t > > predicate_t>
static auto all (const source_t &source, predicate_t &&predicatepredicate) -> bool
 Determines whether all elements of a sequence satisfy a condition.
template<xtd::iterable source_t>
static auto any (const source_t &source) noexcept -> bool
 Determines whether a sequence contains any elements.
template<xtd::iterable source_t, xtd::predicate_callable< xtd::iterable_value_type< source_t > > predicate_t>
static auto any (const source_t &source, predicate_t &&predicatepredicate) -> bool
 Determines whether any element of a sequence satisfies a condition.
template<xtd::iterable source_t>
static auto append (source_t &&source, xtd::iterable_value_type< source_t > &&element) noexcept -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
 Appends a value to the end of the sequence.
template<xtd::iterable source_t>
static auto as_enumerable (source_t &&source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<xtd::iterable source_t>
static auto as_enumerable (const source_t &source) noexcept -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<xtd::iterable source_t>
static auto as_enumerable (source_t &source) noexcept -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename value_t>
static auto as_enumerable (std::initializer_list< value_t > source) noexcept -> xtd::collections::generic::enumerable_generator< value_t >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<std::forward_iterator iterator_t>
static auto as_enumerable (iterator_t first, iterator_t last) noexcept -> xtd::collections::generic::enumerable_generator< typename std::decay< decltype(*first)>::type >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<std::forward_iterator iterator_t>
static auto as_enumerable (iterator_t iteratoriterator, xtd::usize length) noexcept -> xtd::collections::generic::enumerable_generator< typename std::decay< decltype(*iteratoriterator)>::type >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename value_t, xtd::usize length>
static auto as_enumerable (const value_t(&array)[length]) noexcept -> xtd::collections::generic::enumerable_generator< value_t >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename value_t, xtd::usize length>
static auto as_enumerable (value_t(&array)[length]) noexcept -> xtd::collections::generic::enumerable_generator< value_t >
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<xtd::iterable source_t>
requires xtd::real_decimal<xtd::iterable_value_type<source_t>>
static auto average (const source_t &source) -> xtd::decimal
 Computes the average of a sequence of xtd::decimal values.
template<xtd::iterable source_t>
requires xtd::real_double<xtd::iterable_value_type<source_t>>
static auto average (const source_t &source) -> double
 Computes the average of a sequence of double values.
template<xtd::iterable source_t>
requires xtd::real_single<xtd::iterable_value_type<source_t>>
static auto average (const source_t &source) -> xtd::single
 Computes the average of a sequence of float values.
template<xtd::iterable source_t>
requires xtd::signed_integer_32<xtd::iterable_value_type<source_t>>
static auto average (const source_t &source) -> double
 Computes the average of a sequence of xtd::int32 values.
template<xtd::iterable source_t>
requires xtd::signed_integer_64<xtd::iterable_value_type<source_t>>
static auto average (const source_t &source) -> double
 Computes the average of a sequence of xtd::int64 values.
template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::decimal>>
static auto average (const source_t &source) -> xtd::optional< xtd::decimal >
 Computes the average of a sequence of optional xtd::decimal values.
template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<double>>
static auto average (const source_t &source) -> xtd::optional< double >
 Computes the average of a sequence of optional double values.
template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::single>>
static auto average (const source_t &source) -> xtd::optional< xtd::single >
 Computes the average of a sequence of optional float values.
template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::int32>>
static auto average (const source_t &source) -> xtd::optional< double >
 Computes the average of a sequence of optional xtd::int32 values.
template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::int64>>
static auto average (const source_t &source) -> xtd::optional< double >
 Computes the average of a sequence of optional xtd::int64 values.
template<typename result_t, xtd::iterable source_t>
static auto cast (source_t &&source) -> xtd::collections::generic::enumerable_generator< result_t >
 Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
template<xtd::iterable source_t>
static auto chunk (source_t &&source, xtd::usize size) -> xtd::collections::generic::enumerable_generator< xtd::array< xtd::iterable_value_type< source_t > > >
 Splits the elements of a sequence into chunks of size at most size.
template<xtd::iterable first_t, xtd::iterable second_t>
static auto concat (first_t &&first, second_t &&second) noexcept -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< first_t > >
 Concatenates two sequences.
template<xtd::iterable source_t>
static auto contains (const source_t &source, const xtd::iterable_value_type< source_t > &value) noexcept -> bool
 Determines whether a sequence contains a specified element by using the default equality comparer.
template<xtd::iterable source_t>
static auto contains (const source_t &source, const xtd::iterable_value_type< source_t > &value, const iequality_comparer< xtd::iterable_value_type< source_t > > &comparer) noexcept -> bool
 Determines whether a sequence contains a specified element by using a specified equality comparer.
template<xtd::iterable source_t, xtd::func_callable< bool, xtd::iterable_value_type< source_t >, xtd::iterable_value_type< source_t > > equater_t>
static auto contains (const source_t &source, const xtd::iterable_value_type< source_t > &value, equater_t &&equater) noexcept -> bool
 Determines whether a sequence contains a specified element by using a specified equality comparer.
template<xtd::iterable source_t>
static auto count (const source_t &source) noexcept -> xtd::usize
 Returns the number of elements in a sequence.
template<xtd::iterable source_t, xtd::predicate_callable< xtd::iterable_value_type< source_t > > predicate_t>
static auto count (const source_t &source, predicate_t &&predicatepredicate) noexcept -> xtd::usize
 Returns a number that represents how many elements in the specified sequence satisfy a condition.
template<xtd::iterable source_t>
static auto count (const source_t &source, xtd::iterable_value_type< source_t > &&value) noexcept -> xtd::usize
 Returns the number of elements with the specified value.
template<typename key_t, xtd::iterable source_t, xtd::callable< key_t, xtd::iterable_value_type< source_t > > key_selector_t>
static auto count_by (const source_t &source, key_selector_t &&key_selector) noexcept -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
 Returns the count of elements in the source sequence grouped by key.
template<typename key_t, xtd::iterable source_t, xtd::callable< key_t, xtd::iterable_value_type< source_t > > key_selector_t>
static auto count_by (const source_t &source, key_selector_t &&key_selector, const iequality_comparer< key_t > &key_comparer) noexcept -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
 Returns the count of elements in the source sequence grouped by key.
template<typename key_t, xtd::iterable source_t, xtd::callable< key_t, xtd::iterable_value_type< source_t > > key_selector_t, xtd::callable< bool, key_t, key_t > key_equater_t>
static auto count_by (const source_t &source, key_selector_t &&key_selector, key_equater_t &&key_equater) noexcept -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
 Returns the count of elements in the source sequence grouped by key.
template<typename source_t>
static auto default_if_empty (const ienumerable< source_t > &source) noexcept
 Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
template<typename source_t>
static auto default_if_empty (const ienumerable< source_t > &source, const source_t &default_value) noexcept
 Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.
template<typename source_t>
static auto distinct (const ienumerable< source_t > &source) noexcept
 Returns distinct elements from a sequence by using the default equality comparer to compare values.
template<typename source_t>
static auto distinct (const ienumerable< source_t > &source, const iequality_comparer< source_t > &comparer) noexcept
 Returns distinct elements from a sequence by using a specified xtd::collections::generic::iequality_comparer <type_t> to compare values.
template<typename source_t>
static auto first_or_default (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicatepredicate, const source_t &default_value) noexcept -> source_t
 Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
template<typename source_t>
static auto first_or_default (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicatepredicate) noexcept -> source_t
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
template<typename source_t>
static auto first_or_default (const ienumerable< source_t > &source, const source_t &default_value) noexcept -> source_t
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
template<typename source_t>
static auto first_or_default (const ienumerable< source_t > &source) noexcept -> source_t
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
template<typename source_t>
static const auto & from (const ienumerable< source_t > &source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename source_t>
static auto from (std::initializer_list< source_t > source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename collection_t>
static auto from (const collection_t &source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename input_iterator_t>
static auto from (input_iterator_t first, input_iterator_t last) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename input_iterator_t>
static auto from (input_iterator_t iteratoriterator, xtd::usize length) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename source_t, xtd::usize length>
static auto from (const source_t(&array)[length]) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
template<typename result_t, typename source_t>
requires xtd::numeric<result_t>
static auto max (const ienumerable< source_t > &source, auto &&selector)
template<typename source_t>
requires xtd::numeric<source_t>
static auto max (const ienumerable< source_t > &source, auto &&selector)
template<typename source_t>
requires xtd::numeric<source_t>
static auto max (const ienumerable< source_t > &source)
template<typename result_t, typename source_t>
requires xtd::numeric<result_t>
static auto min (const ienumerable< source_t > &source, auto &&selector)
template<typename source_t>
requires xtd::numeric<source_t>
static auto min (const ienumerable< source_t > &source, auto &&selector)
template<typename source_t>
requires xtd::numeric<source_t>
static auto min (const ienumerable< source_t > &source)
template<typename source_t>
static auto order (const ienumerable< source_t > &source)
 Sorts the elements of a sequence in ascending order.
template<typename source_t>
static auto order (const ienumerable< source_t > &source, const xtd::collections::generic::icomparer< source_t > &comparer)
 Sorts the elements of a sequence in ascending order.
template<typename key_t, typename source_t>
static auto order_by (const ienumerable< source_t > &source, const std::function< key_t(const source_t &)> &key_selector)
 Sorts the elements of a sequence in ascending order according to a key.
template<typename source_t>
static auto order_by (const ienumerable< source_t > &source, const std::function< source_t(const source_t &)> &key_selector)
 Sorts the elements of a sequence in ascending order according to a key.
template<typename key_t, typename source_t>
static auto order_by_descending (const ienumerable< source_t > &source, const std::function< key_t(const source_t &)> &key_selector)
 Sorts the elements of a sequence in descending order according to a key.
template<typename source_t>
static auto order_by_descending (const ienumerable< source_t > &source, const std::function< source_t(const source_t &)> &key_selector)
 Sorts the elements of a sequence in descending order according to a key.
template<typename type_t>
static auto range (type_t count)
 Generates a sequence of integral numbers within a specified range.
template<typename type_t>
static auto range (type_t start, type_t count)
 Generates a sequence of integral numbers within a specified range.
template<typename type_t>
static auto range (type_t start, type_t count, type_t step)
 Generates a sequence of integral numbers within a specified range and step.
template<typename result_t, xtd::iterable source_t>
static auto select (source_t &&source, auto &&selector) -> xtd::collections::generic::enumerable_generator< result_t >
 Projects each element of a sequence into a new form.
template<xtd::iterable source_t>
static auto select (source_t &&source, auto &&selector) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
 Projects each element of a sequence into a new form by incorporating the element's index.
template<typename source_t>
static auto to_array (const ienumerable< source_t > &source)
 Creates a xtd::array <type_t> from an xtd::collections::generic::ienumerable <type_t>.
template<typename source_t>
static auto to_list (const ienumerable< source_t > &source)
 Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
template<typename source_t>
static auto where (const ienumerable< source_t > &source, auto &&predicatepredicate)
 Filters a sequence of values based on a predicate.

Member Typedef Documentation

◆ enumerator

template<typename type_t>
using xtd::linq::enumerable::enumerator

Represents the enumerator value type.

◆ iequality_comparer

template<typename type_t>
using xtd::linq::enumerable::iequality_comparer

Represents the ienumerable value type.

◆ ienumerable

template<typename type_t>
using xtd::linq::enumerable::ienumerable

Represents the ienumerable value type.

◆ list

template<typename type_t>
using xtd::linq::enumerable::list

Represents the list value type.

◆ key_value_pair

template<typename key_t, typename value_t>
using xtd::linq::enumerable::key_value_pair

Represents the key value pair value type.

Member Function Documentation

◆ aggregate() [1/3]

template<xtd::iterable source_t, xtd::func_callable< xtd::iterable_value_type< source_t >, xtd::iterable_value_type< source_t >, xtd::iterable_value_type< source_t > > func_t>
auto xtd::linq::enumerable::aggregate ( const source_t & source,
func_t && func ) -> xtd::iterable_value_type< source_t >
staticnodiscard

Applies an accumulator function over a sequence.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceAn xtd::collections::generic::ienumerable <type_t> to aggregate over.
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/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(_2 + " " + _1);
console::write_line(reversed.quoted());
}
// This code produces the following output :
//
// "dog lazy the over jumps fox brown quick the"
static auto write_line() -> void
Writes the current line terminator to the standard output stream using the specified format informati...

◆ aggregate() [2/3]

template<typename accumulate_t, xtd::iterable source_t, xtd::func_callable< accumulate_t, accumulate_t, xtd::iterable_value_type< source_t > > func_t>
auto xtd::linq::enumerable::aggregate ( const source_t & source,
accumulate_t && seed,
func_t && func ) -> accumulate_t
staticnodiscard

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.
source_tThe type of the elements of source.
Parameters
sourceAn xtd::collections::generic::ienumerable <type_t> to aggregate over.
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/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, [](auto&& total, auto&& 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:64
auto aggregate(func_t &&func) const -> value_t
Applies an accumulator function over a sequence.
Definition enumerable.hpp:60

◆ aggregate() [3/3]

template<typename result_t, typename accumulate_t, xtd::iterable source_t, xtd::func_callable< accumulate_t, accumulate_t, xtd::iterable_value_type< source_t > > func_t, xtd::func_callable< result_t, accumulate_t > result_selector_t>
auto xtd::linq::enumerable::aggregate ( const source_t & source,
accumulate_t && seed,
func_t && func,
result_selector_t && result_selector ) -> result_t
staticnodiscard

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.
source_tThe type of the elements of source.
Parameters
sourceAn xtd::collections::generic::ienumerable <type_t> to aggregate over.
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/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",
[](auto&& longest, auto&& next) {return next.length() > longest.length() ? next : longest;},
// Return the final result as an upper case string.
[](auto&& 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<xtd::iterable source_t, xtd::predicate_callable< xtd::iterable_value_type< source_t > > predicate_t>
auto xtd::linq::enumerable::all ( const source_t & source,
predicate_t && predicate ) -> bool
staticnodiscard

Determines whether all elements of a sequence satisfy a condition.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceAn xtd::collections::generic::ienumerable <type_t> that contains the elements to apply the predicate to.
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/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([](auto&& 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'.
auto all(predicate_t &&predicate) const -> bool
Determines whether all elements of a sequence satisfy a condition.
Definition enumerable.hpp:97
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45

◆ any() [1/2]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::any ( const source_t & source) -> bool
staticnodiscardnoexcept

Determines whether a sequence contains any elements.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceThe xtd::collections::generic::ienumerable <type_t> to check for emptiness.
lastThe last iterator.
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/xtd>
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::collections::generic::list< type_t > list
Represents the list value type.
Definition enumerable_.hpp:92

◆ any() [2/2]

template<xtd::iterable source_t, xtd::predicate_callable< xtd::iterable_value_type< source_t > > predicate_t>
auto xtd::linq::enumerable::any ( const source_t & source,
predicate_t && predicate ) -> bool
staticnodiscard

Determines whether any element of a sequence satisfies a condition.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceAn xtd::collections::generic::ienumerable <type_t> that contains the elements to apply the predicate to.
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/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([](auto&& 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<xtd::iterable source_t>
auto xtd::linq::enumerable::append ( source_t && source,
xtd::iterable_value_type< source_t > && element ) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
staticnodiscardnoexcept

Appends a value to the end of the sequence.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
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/xtd>
auto main() -> int {
// Creating a list of numbers
auto numbers = list {1, 2, 3, 4};
// Trying to append any value of the same type
auto __ = 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 auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1258

◆ as_enumerable() [1/8]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::as_enumerable ( source_t && source)
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
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.

◆ as_enumerable() [2/8]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::as_enumerable ( const source_t & source) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
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.

◆ as_enumerable() [3/8]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::as_enumerable ( source_t & source) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
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.

◆ as_enumerable() [4/8]

template<typename value_t>
auto xtd::linq::enumerable::as_enumerable ( std::initializer_list< value_t > source) -> xtd::collections::generic::enumerable_generator< value_t >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
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.

◆ as_enumerable() [5/8]

template<std::forward_iterator iterator_t>
auto xtd::linq::enumerable::as_enumerable ( iterator_t first,
iterator_t last ) -> xtd::collections::generic::enumerable_generator< typename std::decay< decltype(*first)>::type >
staticnodiscardnoexcept

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

Template Parameters
input_iterator_tThe type of the source iterators.
Parameters
firstThe first iterator.
lastThe last iterator.
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.

◆ as_enumerable() [6/8]

template<std::forward_iterator iterator_t>
auto xtd::linq::enumerable::as_enumerable ( iterator_t iterator,
xtd::usize length ) -> xtd::collections::generic::enumerable_generator< typename std::decay< decltype(*iterator)>::type >
staticnodiscardnoexcept

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

Template Parameters
input_iterator_tThe type of the source iterators.
Parameters
iteratorThe iterator.
legnthThe le,gth to iterate.
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.

◆ as_enumerable() [7/8]

template<typename value_t, xtd::usize length>
auto xtd::linq::enumerable::as_enumerable ( const value_t(&) array[length]) -> xtd::collections::generic::enumerable_generator< value_t >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the source array.
Parameters
arrayThe native array.
legnthThe length of the array.
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.

◆ as_enumerable() [8/8]

template<typename value_t, xtd::usize length>
auto xtd::linq::enumerable::as_enumerable ( value_t(&) array[length]) -> xtd::collections::generic::enumerable_generator< value_t >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the source array.
Parameters
arrayThe native array.
legnthThe length of the array.
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() [1/10]

template<xtd::iterable source_t>
requires xtd::real_decimal<xtd::iterable_value_type<source_t>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::decimal
staticnodiscard

Computes the average of a sequence of xtd::decimal values.

Parameters
sourceA sequence of xtd::decimal values to calculate the average of.
Returns
The average of the sequence of values.
Exceptions
xtd::invalid_operation_exception`source` contains no elements.

◆ average() [2/10]

template<xtd::iterable source_t>
requires xtd::real_double<xtd::iterable_value_type<source_t>>
auto xtd::linq::enumerable::average ( const source_t & source) -> double
staticnodiscard

Computes the average of a sequence of double values.

Parameters
sourceA sequence of double values to calculate the average of.
Returns
The average of the sequence of values.
Exceptions
xtd::invalid_operation_exception`source` contains no elements.

◆ average() [3/10]

template<xtd::iterable source_t>
requires xtd::real_single<xtd::iterable_value_type<source_t>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::single
staticnodiscard

Computes the average of a sequence of float values.

Parameters
sourceA sequence of float values to calculate the average of.
Returns
The average of the sequence of values.
Exceptions
xtd::invalid_operation_exception`source` contains no elements.

◆ average() [4/10]

template<xtd::iterable source_t>
requires xtd::signed_integer_32<xtd::iterable_value_type<source_t>>
auto xtd::linq::enumerable::average ( const source_t & source) -> double
staticnodiscard

Computes the average of a sequence of xtd::int32 values.

Parameters
sourceA sequence of xtd::int32 values to calculate the average of.
Returns
The average of the sequence of values.
Exceptions
xtd::invalid_operation_exception`source` contains no elements.

◆ average() [5/10]

template<xtd::iterable source_t>
requires xtd::signed_integer_64<xtd::iterable_value_type<source_t>>
auto xtd::linq::enumerable::average ( const source_t & source) -> double
staticnodiscard

Computes the average of a sequence of xtd::int64 values.

Parameters
sourceA sequence of xtd::int64 values to calculate the average of.
Returns
The average of the sequence of values.
Exceptions
xtd::invalid_operation_exception`source` contains no elements.

◆ average() [6/10]

template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::decimal>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::optional< xtd::decimal >
staticnodiscard

Computes the average of a sequence of optional xtd::decimal values.

Parameters
sourceA sequence of optional xtd::decimal values to calculate the average of.
Returns
The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.

◆ average() [7/10]

template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<double>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::optional< double >
staticnodiscard

Computes the average of a sequence of optional double values.

Parameters
sourceA sequence of optional double values to calculate the average of.
Returns
The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.

◆ average() [8/10]

template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::single>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::optional< xtd::single >
staticnodiscard

Computes the average of a sequence of optional float values.

Parameters
sourceA sequence of optional float values to calculate the average of.
Returns
The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.

◆ average() [9/10]

template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::int32>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::optional< double >
staticnodiscard

Computes the average of a sequence of optional xtd::int32 values.

Parameters
sourceA sequence of optional xtd::int32 values to calculate the average of.
Returns
The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.

◆ average() [10/10]

template<xtd::iterable source_t>
requires std::same_as<xtd::iterable_value_type<source_t>, xtd::optional<xtd::int64>>
auto xtd::linq::enumerable::average ( const source_t & source) -> xtd::optional< double >
staticnodiscard

Computes the average of a sequence of optional xtd::int64 values.

Parameters
sourceA sequence of optional xtd::int64 values to calculate the average of.
Returns
The average of the sequence of values, or xtd::nullopt if the source sequence is empty or contains only values that are xtd::nullopt.

◆ cast()

template<typename result_t, xtd::iterable source_t>
auto xtd::linq::enumerable::cast ( source_t && source) -> xtd::collections::generic::enumerable_generator< result_t >
staticnodiscard

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

Template Parameters
result_tThe type of the resulting value.
source_tThe type of the elements of source.
Parameters
sourceThe xtd::collections::generic::ienumerable that contains the elements to be cast to type result_t.
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`.
Remarks
The xtd::as include file #include <xtd/as> is needeed to use this method.

◆ chunk()

template<xtd::iterable source_t>
auto xtd::linq::enumerable::chunk ( source_t && source,
xtd::usize size ) -> xtd::collections::generic::enumerable_generator< xtd::array< xtd::iterable_value_type< source_t > > >
staticnodiscard

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. @zxception xtd::argument_out_of_range_exception size is equal to 0.

◆ concat()

template<xtd::iterable first_t, xtd::iterable second_t>
auto xtd::linq::enumerable::concat ( first_t && first,
second_t && second ) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< first_t > >
staticnodiscardnoexcept

Concatenates two sequences.

Template Parameters
source_tThe type of the elements of source.
Parameters
firstThe first sequence to concatenate.
secondThe sequence to concatenate to the first sequence.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains the concatenated elements of the two input sequences.

◆ contains() [1/3]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::contains ( const source_t & source,
const xtd::iterable_value_type< source_t > & value ) -> bool
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence in which to locate a value.
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/3]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::contains ( const source_t & source,
const xtd::iterable_value_type< source_t > & value,
const iequality_comparer< xtd::iterable_value_type< source_t > > & comparer ) -> bool
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence in which to locate a value.
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.

◆ contains() [3/3]

template<xtd::iterable source_t, xtd::func_callable< bool, xtd::iterable_value_type< source_t >, xtd::iterable_value_type< source_t > > equater_t>
auto xtd::linq::enumerable::contains ( const source_t & source,
const xtd::iterable_value_type< source_t > & value,
equater_t && equater ) -> bool
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence in which to locate a value.
valueThe value to locate in the sequence.
equaterAn 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<xtd::iterable source_t>
auto xtd::linq::enumerable::count ( const source_t & source) -> xtd::usize
staticnodiscardnoexcept

Returns the number of elements in a sequence.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence that contains elements to be counted.
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>
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<xtd::iterable source_t, xtd::predicate_callable< xtd::iterable_value_type< source_t > > predicate_t>
auto xtd::linq::enumerable::count ( const source_t & source,
predicate_t && predicate ) -> xtd::usize
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence that contains elements to be tested and counted.
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>
struct pet {
string name;
bool vaccinated = false;
};
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(_*member(&pet::vaccinated) == false);
console::write_line("There are {} unvaccinated animals.", number_unvaccinated);
}
// This code produces the following output :
//
// There are 2 unvaccinated animals.
auto count() const noexcept -> size_type override
Gets the number of elements contained in the xtd::array <type_t>.
Definition basic_array.hpp:72
constexpr auto member(member_t member)
The xtd::expressions::member is use to bind object member.
Definition member_expression.hpp:43

◆ count() [3/3]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::count ( const source_t & source,
xtd::iterable_value_type< source_t > && value ) -> xtd::usize
staticnodiscardnoexcept

Returns the number of elements with the specified value.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence that contains elements to be tested and counted.
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/3]

template<typename key_t, xtd::iterable source_t, xtd::callable< key_t, xtd::iterable_value_type< source_t > > key_selector_t>
auto xtd::linq::enumerable::count_by ( const source_t & source,
key_selector_t && key_selector ) -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
key_tThe type of the key returned by key_selector.
Parameters
sourceA sequence that contains elements to be counted.
key_selectorA function to extract the key for each element.
Returns
An enumerable containing the frequencies of each key occurrence in source.
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>
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>(_*member(&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
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
virtual auto equals(const type_t &) const noexcept -> bool=0
Indicates whether the current object is equal to another object of the same type.
constexpr auto _
The xtd::expressions::_ placeholder instance is used by expression that have only one argument.
Definition args.hpp:66
@ query
The xtd::uri::query data.
Definition uri_components.hpp:29
@ other
The operating system is other.
Definition platform_id.hpp:60

◆ count_by() [2/3]

template<typename key_t, xtd::iterable source_t, xtd::callable< key_t, xtd::iterable_value_type< source_t > > key_selector_t>
auto xtd::linq::enumerable::count_by ( const source_t & source,
key_selector_t && key_selector,
const iequality_comparer< key_t > & key_comparer ) -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
key_tThe type of the key returned by key_selector.
Parameters
sourceA sequence that contains elements to be counted.
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 source.
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>
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>(_*member(&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

◆ count_by() [3/3]

template<typename key_t, xtd::iterable source_t, xtd::callable< key_t, xtd::iterable_value_type< source_t > > key_selector_t, xtd::callable< bool, key_t, key_t > key_equater_t>
auto xtd::linq::enumerable::count_by ( const source_t & source,
key_selector_t && key_selector,
key_equater_t && key_equater ) -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
staticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
key_tThe type of the key returned by key_selector.
Parameters
sourceA sequence that contains elements to be counted.
key_selectorA function to extract the key for each element.
key_equaterAn equality comparer to compare keys.
Returns
An enumerable containing the frequencies of each key occurrence in source.
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>
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>(_*member(&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<typename source_t>
auto xtd::linq::enumerable::default_if_empty ( const ienumerable< source_t > & source)
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceThe sequence to return a default value for if it is empty.
Returns
An xtd::collections::generic::ienumerable <type_t> that contains default_value if source is empty; otherwise, source.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::default_if_empty <source_t>(const ienumerable <source_t>&) to return a default value if a sequence is empty.
#include <xtd/xtd>
struct pet {
string name;
int age = 0;
friend bool operator ==(const pet& lhs, const pet& rhs) noexcept {
return lhs.name == rhs.name && lhs.age == rhs.age;
}
};
auto main() -> int {
auto pets1 = array<pet> {
{.name="Barley", .age=8},
{.name="Boots", .age=4},
{.name="Whiskers", .age=1}
};
for (const auto& [name, age] : pets1.default_if_empty())
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();
auto pets2 = array<pet> {};
for (const auto& [name, age] : pets2.default_if_empty())
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();
}
// This code produces the following output :
//
// pet {name=Barley, age=8}
// pet {name=Boots, age=4}
// pet {name=Whiskers, age=1}
//
// pet {name=, age=0}
//

◆ default_if_empty() [2/2]

template<typename source_t>
auto xtd::linq::enumerable::default_if_empty ( const ienumerable< source_t > & source,
const source_t & default_value )
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceThe sequence to return a default value for if it is empty.
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.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::default_if_empty <source_t>(const ienumerable <source_t>&, const source_t&) to return a default value if a sequence is empty.
#include <xtd/xtd>
struct pet {
string name;
int age = 0;
static pet default_pet;
friend bool operator ==(const pet& lhs, const pet& rhs) noexcept {
return lhs.name == rhs.name && lhs.age == rhs.age;
}
};
pet pet::default_pet {.name = "Default pet", .age = 0};
auto main() -> int {
auto pets1 = array<pet> {
{.name="Barley", .age=8},
{.name="Boots", .age=4},
{.name="Whiskers", .age=1}
};
for (const auto& [name, age] : pets1.default_if_empty(pet::default_pet))
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();
auto pets2 = array<pet> {};
for (const auto& [name, age] : pets2.default_if_empty(pet::default_pet))
console::write_line("pet {{name={}, age={}}}", name, age);
console::write_line();
}
// This code produces the following output :
//
// pet {name=Barley, age=8}
// pet {name=Boots, age=4}
// pet {name=Whiskers, age=1}
//
// pet {name=Default pet, age=0}
//

◆ distinct() [1/2]

template<typename source_t>
auto xtd::linq::enumerable::distinct ( const ienumerable< source_t > & source)
inlinestaticnodiscardnoexcept

Returns distinct elements from a sequence by using the default equality comparer to compare values.

Parameters
sourceThe sequence to remove duplicate elements from.
Returns
An enumerable distinct elements from the source sequence.

◆ distinct() [2/2]

template<typename source_t>
auto xtd::linq::enumerable::distinct ( const ienumerable< source_t > & source,
const iequality_comparer< source_t > & comparer )
inlinestaticnodiscardnoexcept

Returns distinct elements from a sequence by using a specified xtd::collections::generic::iequality_comparer <type_t> to compare values.

Parameters
sourceThe sequence to remove duplicate elements from.
comparerAn xtd::collections::generic::iequality_comparer <type_t> to compare values.
Returns
An enumerable distinct elements from the source sequence.

◆ first_or_default() [1/4]

template<typename source_t>
auto xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > & source,
const std::function< bool(const source_t &)> & predicate,
const source_t & default_value ) -> source_t
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to return an element from.
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<typename source_t>
auto xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > & source,
const std::function< bool(const source_t &)> & predicate ) -> source_t
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to return an element from.
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<typename source_t>
auto xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > & source,
const source_t & default_value ) -> source_t
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to return an element from.
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<typename source_t>
auto xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > & source) -> source_t
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to return an element from.
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.

◆ from() [1/6]

template<typename source_t>
const auto & xtd::linq::enumerable::from ( const ienumerable< source_t > & source)
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
Returns
The input sequence typed as xtd::collections::generic::ienumerable <type_t>.
Remarks
Same as as_enemerable(const ienumerable<source_t>& source).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
auto main() -> int {
auto items = array {"One", "Two", "Three", "Four", "Five"};
const auto& enumerable = from(items);
auto enumerator = enumerable.get_enumerator();
while (enumerator.move_next())
}
// This code produces the following output :
//
// One
// Two
// Three
// Four
// Five
auto from(const source_t &source) noexcept
Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
Definition from.hpp:28
const type_t & current() const override
Gets the element in the collection at the current position of the enumerator.
Definition enumerator.hpp:63
bool move_next() override
Advances the enumerator to the next element of the collection.
Definition enumerator.hpp:72

◆ from() [2/6]

template<typename source_t>
auto xtd::linq::enumerable::from ( std::initializer_list< source_t > source)
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values.
Returns
The input sequence typed as xtd::collections::generic::ienumerable <type_t>.
Remarks
Same as as_enemerable(std::initializer_list<source_t> source).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
auto main() -> int {
auto items = std::initializer_list<string> {"One", "Two", "Three", "Four", "Five"};
const auto& enumerable = from(items);
auto enumerator = enumerable.get_enumerator();
while (enumerator.move_next())
}
// This code produces the following output :
//
// One
// Two
// Three
// Four
// Five

◆ from() [3/6]

template<typename collection_t>
auto xtd::linq::enumerable::from ( const collection_t & source)
inlinestaticnodiscardnoexcept

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

Template Parameters
collection_tThe type of the source.
Parameters
sourceA sequence of values.
Returns
The input sequence typed as xtd::collections::generic::ienumerable <type_t>.
Remarks
Same as as_enemerable(const collection_t& source).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
auto main() -> int {
auto items = std::vector {"One", "Two", "Three", "Four", "Five"};
const auto& enumerable = from(items);
auto enumerator = enumerable.get_enumerator();
while (enumerator.move_next())
}
// This code produces the following output :
//
// One
// Two
// Three
// Four
// Five

◆ from() [4/6]

template<typename input_iterator_t>
auto xtd::linq::enumerable::from ( input_iterator_t first,
input_iterator_t last )
inlinestaticnodiscardnoexcept

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

Template Parameters
input_iterator_tThe type of the source iterators.
Parameters
firstThe first iterator.
lastThe last iterator.
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.
Remarks
Same as as_enemerable(input_iterator_t first, input_iterator_t last).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
auto main() -> int {
auto items = std::vector {"One", "Two", "Three", "Four", "Five"};
const auto& enumerable = from(items.begin(), items.end());
auto enumerator = enumerable.get_enumerator();
while (enumerator.move_next())
}
// This code produces the following output :
//
// One
// Two
// Three
// Four
// Five

◆ from() [5/6]

template<typename input_iterator_t>
auto xtd::linq::enumerable::from ( input_iterator_t iterator,
xtd::usize length )
inlinestaticnodiscardnoexcept

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

Template Parameters
input_iterator_tThe type of the source iterators.
Parameters
iteratorThe iterator.
legnthThe le,gth to iterate.
Returns
The input sequence typed as xtd::collections::generic::ienumerable <type_t>.
Remarks
Same as as_enemerable(input_iterator_t iterator, xtd::usize length).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
auto main() -> int {
auto items = std::vector {"One", "Two", "Three", "Four", "Five"};
const auto& enumerable = from(items.begin(), 5);
auto enumerator = enumerable.get_enumerator();
while (enumerator.move_next())
}
// This code produces the following output :
//
// One
// Two
// Three
// Four
// Five

◆ from() [6/6]

template<typename source_t, xtd::usize length>
auto xtd::linq::enumerable::from ( const source_t(&) array[length])
inlinestaticnodiscardnoexcept

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

Template Parameters
source_tThe type of the source array.
Parameters
arrayThe native array.
legnthThe length of the array.
Returns
The input sequence typed as xtd::collections::generic::ienumerable <type_t>.
Remarks
Same as as_enemerable(const source_t (&array)[length]).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
auto main() -> int {
const char* items[] = {"One", "Two", "Three", "Four", "Five"};
const auto& enumerable = from(items);
auto enumerator = enumerable.get_enumerator();
while (enumerator.move_next())
}
// This code produces the following output :
//
// One
// Two
// Three
// Four
// Five

◆ order() [1/2]

template<typename source_t>
auto xtd::linq::enumerable::order ( const ienumerable< source_t > & source)
inlinestaticnodiscard

Sorts the elements of a sequence in ascending order.

Parameters
sourceA sequence of values to order.
Returns
An xtd::collections::generic::ienumerable <source_t> whose elements are sorted.

◆ order() [2/2]

template<typename source_t>
auto xtd::linq::enumerable::order ( const ienumerable< source_t > & source,
const xtd::collections::generic::icomparer< source_t > & comparer )
inlinestaticnodiscard

Sorts the elements of a sequence in ascending order.

Parameters
sourceA sequence of values to order.
comparerAn xtd::collections::generic::icomparer <source_t> to compare keys.
Returns
An xtd::collections::generic::ienumerable <source_t> whose elements are sorted.

◆ order_by() [1/2]

template<typename key_t, typename source_t>
auto xtd::linq::enumerable::order_by ( const ienumerable< source_t > & source,
const std::function< key_t(const source_t &)> & key_selector )
inlinestaticnodiscard

Sorts the elements of a sequence in ascending order according to a key.

Parameters
sourceA sequence of values to order.
key_selectorA function to extract a key from an element.
Examples
The following code example demonstrates how to use order_by<key_t, source_t>(ienumerable <source_t>, std::function<key_t(const source_t&)>) to sort the elements of a sequence.

◆ order_by() [2/2]

template<typename source_t>
auto xtd::linq::enumerable::order_by ( const ienumerable< source_t > & source,
const std::function< source_t(const source_t &)> & key_selector )
inlinestaticnodiscard

Sorts the elements of a sequence in ascending order according to a key.

Parameters
sourceA sequence of values to order.
key_selectorA function to extract a key from an element.
Examples
The following code example demonstrates how to use order_by<source_t, source_t>(ienumerable <source_t>, std::function<source_t(const source_t&)>) to sort the elements of a sequence.

◆ order_by_descending() [1/2]

template<typename key_t, typename source_t>
auto xtd::linq::enumerable::order_by_descending ( const ienumerable< source_t > & source,
const std::function< key_t(const source_t &)> & key_selector )
inlinestaticnodiscard

Sorts the elements of a sequence in descending order according to a key.

Parameters
sourceA sequence of values to order.
key_selectorA function to extract a key from an element.
Examples
The following code example demonstrates how to use order_by<key_t, source_t>(ienumerable <source_t>, std::function<key_t(const source_t&)>) to sort the elements of a sequence.

◆ order_by_descending() [2/2]

template<typename source_t>
auto xtd::linq::enumerable::order_by_descending ( const ienumerable< source_t > & source,
const std::function< source_t(const source_t &)> & key_selector )
inlinestaticnodiscard

Sorts the elements of a sequence in descending order according to a key.

Parameters
sourceA sequence of values to order.
key_selectorA function to extract a key from an element.
Examples
The following code example demonstrates how to use order_by<source_t, source_t>(ienumerable <source_t>, std::function<source_t(const source_t&)>) to sort the elements of a sequence.

◆ range() [1/3]

template<typename type_t>
auto xtd::linq::enumerable::range ( type_t count)
inlinestaticnodiscard

Generates a sequence of integral numbers within a specified range.

Parameters
countThe number of sequential integers to generate.
Returns
An xtd::collections::generic::ienumerable <xtd::int32> that contains a range of sequential integral numbers.
Exceptions
xtd::argument_out_of_range_exception`count` is less than 0.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::range to generate a sequence of values.
#include <xtd/xtd>
auto main() -> int {
const auto& squares = enumerable::range(1, 10).select(_ * _);
for (auto num : squares)
}
// This code produces the following output :
//
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
// 100
static auto range(type_t count)
Generates a sequence of integral numbers within a specified range.
Definition enumerable_.hpp:790

◆ range() [2/3]

template<typename type_t>
auto xtd::linq::enumerable::range ( type_t start,
type_t count )
inlinestaticnodiscard

Generates a sequence of integral numbers within a specified range.

Parameters
startThe value of the first integer in the sequence.
countThe number of sequential integers to generate.
Returns
An xtd::collections::generic::ienumerable <xtd::int32> that contains a range of sequential integral numbers.
Exceptions
xtd::argument_out_of_range_exception`count` is less than 0.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::range to generate a sequence of values.
#include <xtd/xtd>
auto main() -> int {
const auto& squares = enumerable::range(1, 10).select(_ * _);
for (auto num : squares)
}
// This code produces the following output :
//
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
// 100

◆ range() [3/3]

template<typename type_t>
auto xtd::linq::enumerable::range ( type_t start,
type_t count,
type_t step )
inlinestaticnodiscard

Generates a sequence of integral numbers within a specified range and step.

Parameters
startThe value of the first integer in the sequence.
countThe number of sequential integers to generate.
stepThe integer number specifying the incrementation.
Returns
An xtd::collections::generic::ienumerable that contains a range of sequential integral numbers.
Exceptions
xtd::argument_out_of_range_exception`count` is less than 0.

◆ select() [1/2]

template<typename result_t, xtd::iterable source_t>
auto xtd::linq::enumerable::select ( source_t && source,
auto && selector ) -> xtd::collections::generic::enumerable_generator< result_t >
staticnodiscard

Projects each element of a sequence into a new form.

Template Parameters
result_tThe type of the resulting value.
source_tThe type of the elements of source.
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.
#include <xtd/xtd>
auto main() -> int {
auto fruits = array<string> {"apple", "banana", "mango", "orange", "passionfruit", "grape"};
const auto& query = fruits.select<std::tuple<usize, string>>([](auto&& fruit, auto 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() [2/2]

template<xtd::iterable source_t>
auto xtd::linq::enumerable::select ( source_t && source,
auto && selector ) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
staticnodiscard

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.
source_tThe type of the elements of source.
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&, xtd::usize)>&) to project over a sequence of values and use the index of each element.
#include <xtd/xtd>
auto main() -> int {
auto fruits = array<string> {"apple", "banana", "mango", "orange", "passionfruit", "grape"};
const auto& query = fruits.select<std::tuple<usize, string>>([](auto&& fruit, auto 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_array()

template<typename source_t>
auto xtd::linq::enumerable::to_array ( const ienumerable< source_t > & source)
staticnodiscard

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceThe xtd::collections::generic::ienumerable <type_t> to create a xtd::collections::generic::list <type_t> from.
Returns
A xtd::array <type_t> that contains elements from the input sequence.
Remarks
The xtd::collections::generic::list include file #include <xtd/array> is needeed to use this method.
Examples
The following code example demonstrates how to use xtd::linq::enumerable::to_array to force immediate query evaluation and return a xtd::array <type_t> that contains the query results.

◆ to_list()

template<typename source_t>
auto xtd::linq::enumerable::to_list ( const ienumerable< source_t > & source)
staticnodiscard

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceThe xtd::collections::generic::ienumerable <type_t> to create a xtd::collections::generic::list <type_t> from.
Returns
A xtd::collections::generic::list <type_t> that contains elements from the input sequence.
Remarks
The xtd::collections::generic::list include file #include <xtd/collections/generic/lists> is needeed to use this method.
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/xtd>
auto main() -> int {
auto fruits = array<string> {"apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry"};
auto lengths = fruits.select<usize>(_*method(&string::length)).to_list();
for (auto length : lengths)
}
// This code produces the following output :
//
// 5
// 12
// 6
// 5
// 6
// 9
// 5
// 10
virtual auto length() const noexcept -> size_type
Definition basic_string.hpp:383
auto select(auto &&selector) const
Projects each element of a sequence into a new form.
Definition enumerable.hpp:399
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:212

◆ where()

template<typename source_t>
auto xtd::linq::enumerable::where ( const ienumerable< source_t > & source,
auto && predicate )
inlinestaticnodiscard

Filters a sequence of values based on a predicate.

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to filter.
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>
auto main() -> int {
auto fruits = array {"apple"_s, "passionfruit"_s, "banana"_s, "mango"_s, "orange"_s, "blueberry"_s, "grape"_s, "strawberry"_s};
auto query = fruits.where(_*method(&string::length) < 6);
for (const auto& fruit : query)
}
// This code produces the following output :
//
// apple
// mango
// grape
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&, xtd::usize)>&) to filter a sequence based on a predicate that involves the index of each element.
#include <xtd/xtd>
auto main() -> int {
auto numbers = array {0, 30, 20, 15, 90, 85, 40, 75};
auto query = numbers.where(_1 <= _2 * 10);
for (const auto& number : query)
}
// This code produces the following output :
//
// 0
// 20
// 15
// 40
Definition number.hpp:11

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