xtd 0.2.0
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<class type_t>
Provides a set of static methods for querying objects that implement ienumerable <type_t>.
Definition enumerable.hpp:58
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<class type_t >
using enumerator = typename xtd::collections::generic::enumerator< type_t >
 Represents the enumerator value type.
 
template<class type_t >
using iequality_comparer = typename xtd::collections::generic::iequality_comparer< type_t >
 Represents the ienumerable value type.
 
template<class type_t >
using ienumerable = typename xtd::collections::generic::ienumerable< type_t >
 Represents the ienumerable value type.
 
template<class type_t >
using list = typename xtd::collections::generic::list< type_t >
 Represents the list value type.
 
template<class key_t , class value_t >
using key_value_pair = xtd::collections::generic::key_value_pair< key_t, value_t >
 Represents the key value pair value type.
 

Public Static Methods

template<class source_t >
static source_t aggregate (const ienumerable< source_t > &source, const std::function< source_t(const source_t &, const source_t &)> &func)
 Applies an accumulator function over a sequence.
 
template<class source_t >
static source_t aggregate (const ienumerable< source_t > &source, const source_t &seed, const std::function< source_t(const source_t &, const source_t &)> &func)
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
 
template<class accumulate_t , class source_t >
static accumulate_t aggregate (const ienumerable< source_t > &source, const accumulate_t &seed, const std::function< accumulate_t(const source_t &, const accumulate_t &)> &func)
 Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
 
template<class source_t >
static source_t aggregate (const ienumerable< source_t > &source, 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)
 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 , class source_t >
static result_t aggregate (const ienumerable< source_t > &source, 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)
 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 source_t >
static bool all (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicate)
 Determines whether all elements of a sequence satisfy a condition.
 
template<class source_t >
static bool any (const ienumerable< source_t > &source) noexcept
 Determines whether a sequence contains any elements.
 
template<class source_t >
static bool any (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicate)
 Determines whether any element of a sequence satisfies a condition.
 
template<class source_t >
static const ienumerable< source_t > & append (const ienumerable< source_t > &source, const source_t &element) noexcept
 Appends a value to the end of the sequence.
 
template<class source_t >
static const ienumerable< source_t > & as_enumerable (const ienumerable< source_t > &source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class source_t >
static const ienumerable< source_t > & as_enumerable (std::initializer_list< source_t > source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class collection_t >
static const auto & as_enumerable (const collection_t &source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class input_iterator_t >
static const auto & as_enumerable (input_iterator_t first, input_iterator_t last) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class input_iterator_t >
static const auto & as_enumerable (input_iterator_t iterator, size_t length) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class source_t , size_t length>
static const auto & as_enumerable (const source_t(&array)[length]) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
static xtd::decimal average (const ienumerable< xtd::decimal > &source)
 Computes the average of a sequence of xtd::decimal values.
 
static double average (const ienumerable< double > &source)
 Computes the average of a sequence of double values.
 
static float average (const ienumerable< float > &source)
 Computes the average of a sequence of float values.
 
static double average (const ienumerable< xtd::int32 > &source)
 Computes the average of a sequence of xtd::int32 values.
 
static double average (const ienumerable< xtd::int64 > &source)
 Computes the average of a sequence of xtd::int64 values.
 
static xtd::optional< xtd::decimalaverage (const ienumerable< xtd::optional< xtd::decimal > > &source) noexcept
 Computes the average of a sequence of optional xtd::decimal values.
 
static xtd::optional< double > average (const ienumerable< xtd::optional< double > > &source) noexcept
 Computes the average of a sequence of optional double values.
 
static xtd::optional< float > average (const ienumerable< xtd::optional< float > > &source) noexcept
 Computes the average of a sequence of optional float values.
 
static xtd::optional< double > average (const ienumerable< xtd::optional< xtd::int32 > > &source) noexcept
 Computes the average of a sequence of optional xtd::int32 values.
 
static xtd::optional< double > average (const ienumerable< xtd::optional< xtd::int64 > > &source) noexcept
 Computes the average of a sequence of optional xtd::int64 values.
 
template<class result_t , class source_t >
static const ienumerable< result_t > & cast (const ienumerable< source_t > &source) noexcept
 Casts the elements of an xtd::collections::generic::ienumerable to the specified type.
 
template<class source_t >
static const ienumerable< xtd::array< source_t > > & chunk (const ienumerable< source_t > &source, xtd::size size)
 Splits the elements of a sequence into chunks of size at most size.
 
template<class source_t >
static const ienumerable< source_t > & concat (const ienumerable< source_t > &first, const ienumerable< source_t > &second) noexcept
 Concatenates two sequences.
 
template<class source_t >
static bool contains (const ienumerable< source_t > &source, const source_t &value) noexcept
 Determines whether a sequence contains a specified element by using the default equality comparer.
 
template<class source_t >
static bool contains (const ienumerable< source_t > &source, const source_t &value, const xtd::collections::generic::iequality_comparer< source_t > &comparer) noexcept
 Determines whether a sequence contains a specified element by using a specified equality comparer.
 
template<class source_t >
static xtd::size count (const ienumerable< source_t > &source) noexcept
 Returns the number of elements in a sequence.
 
template<class source_t >
static xtd::size count (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicate) noexcept
 Returns a number that represents how many elements in the specified sequence satisfy a condition.
 
template<class source_t >
static xtd::size count (const ienumerable< source_t > &source, const source_t &value) noexcept
 Returns the number of elements with the specified value.
 
template<class key_t , class source_t >
static const ienumerable< key_value_pair< key_t, xtd::size > > & count_by (const ienumerable< source_t > &source, const std::function< key_t(const source_t &)> &key_selector) noexcept
 Returns the count of elements in the source sequence grouped by key.
 
template<class key_t , class source_t >
static const ienumerable< key_value_pair< key_t, xtd::size > > & count_by (const ienumerable< source_t > &source, const std::function< key_t(const source_t &)> &key_selector, const iequality_comparer< key_t > &key_comparer) noexcept
 Returns the count of elements in the source sequence grouped by key.
 
template<class source_t >
static const ienumerable< source_t > & 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<class source_t >
static const ienumerable< source_t > & 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<class source_t >
static source_t first_or_default (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicate, const source_t &default_value) noexcept
 Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
 
template<class source_t >
static source_t first_or_default (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicate) noexcept
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
 
template<class source_t >
static source_t first_or_default (const ienumerable< source_t > &source, const source_t &default_value) noexcept
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
 
template<class source_t >
static source_t first_or_default (const ienumerable< source_t > &source) noexcept
 Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
 
template<class type_t >
static const ienumerable< type_t > & range (type_t count)
 Generates a sequence of integral numbers within a specified range.
 
template<class type_t >
static const ienumerable< type_t > & range (type_t start, type_t count)
 Generates a sequence of integral numbers within a specified range.
 
template<class type_t >
static const ienumerable< type_t > & range (type_t start, type_t count, type_t step)
 Generates a sequence of integral numbers within a specified range and step.
 
template<class source_t >
static const ienumerable< source_t > & from (const ienumerable< source_t > &source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class source_t >
static const ienumerable< source_t > & from (std::initializer_list< source_t > source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class collection_t >
static const auto & from (const collection_t &source) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class input_iterator_t >
static const auto & from (input_iterator_t first, input_iterator_t last) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class input_iterator_t >
static const auto & from (input_iterator_t iterator, size_t length) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class source_t , size_t length>
static const auto & from (const source_t(&array)[length]) noexcept
 Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
 
template<class result_t , class source_t >
static const ienumerable< result_t > & select (const ienumerable< source_t > &source, const std::function< result_t(const source_t &)> &selector)
 Projects each element of a sequence into a new form.
 
template<class source_t >
static const ienumerable< source_t > & select (const ienumerable< source_t > &source, const std::function< source_t(const source_t &)> &selector)
 Projects each element of a sequence into a new form.
 
template<class result_t , class source_t >
static const ienumerable< result_t > & select (const ienumerable< source_t > &source, const std::function< result_t(const source_t &, xtd::size)> &selector)
 Projects each element of a sequence into a new form by incorporating the element's index.
 
template<class source_t >
static const ienumerable< source_t > & select (const ienumerable< source_t > &source, const std::function< source_t(const source_t &, xtd::size)> &selector)
 Projects each element of a sequence into a new form by incorporating the element's index.
 
template<class source_t >
static const list< source_t > & to_list (const ienumerable< source_t > &source) noexcept
 Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
 
template<class source_t >
static const ienumerable< source_t > & where (const ienumerable< source_t > &source, const std::function< bool(const source_t &)> &predicate)
 Filters a sequence of values based on a predicate.
 
template<class source_t >
static const ienumerable< source_t > & where (const ienumerable< source_t > &source, const std::function< bool(const source_t &, xtd::size)> &predicate)
 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

◆ enumerator

template<class type_t >
using xtd::linq::enumerable::enumerator = typename xtd::collections::generic::enumerator<type_t>

Represents the enumerator value type.

◆ iequality_comparer

Represents the ienumerable value type.

◆ ienumerable

template<class type_t >
using xtd::linq::enumerable::ienumerable = typename xtd::collections::generic::ienumerable<type_t>

Represents the ienumerable value type.

◆ list

template<class type_t >
using xtd::linq::enumerable::list = typename xtd::collections::generic::list<type_t>

Represents the list value type.

◆ key_value_pair

template<class key_t , class value_t >
using xtd::linq::enumerable::key_value_pair = xtd::collections::generic::key_value_pair<key_t, value_t>

Represents the key value pair value type.

Member Function Documentation

◆ aggregate() [1/5]

template<class source_t >
static source_t xtd::linq::enumerable::aggregate ( const ienumerable< source_t > &  source,
const std::function< source_t(const source_t &, const source_t &)> &  func 
)
inlinestatic

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/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 source_t >
static source_t xtd::linq::enumerable::aggregate ( const ienumerable< source_t > &  source,
const source_t &  seed,
const std::function< source_t(const source_t &, const source_t &)> &  func 
)
inlinestatic

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

Template Parameters
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/array>
#include <xtd/console>
using namespace xtd;
auto main() -> int {
auto ints = array {4, 8, 8, 3, 9, 0, 7, 8, 2};
// Count the even numbers in the array, using a seed value of 0.
auto num_even = ints.aggregate(0, [](int total, int next) {
return next % 2 == 0 ? total + 1 : total;
});
console::write_line("The number of even integers is: {}", num_even);
}
// This code produces the following output :
//
// The number of even integers is: 6
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
source_t aggregate(const std::function< source_t(const source_t &, const source_t &)> &func) const
Applies an accumulator function over a sequence.
Definition enumerable.hpp:59

◆ aggregate() [3/5]

template<class accumulate_t , class source_t >
static accumulate_t xtd::linq::enumerable::aggregate ( const ienumerable< source_t > &  source,
const accumulate_t &  seed,
const std::function< accumulate_t(const source_t &, const accumulate_t &)> &  func 
)
inlinestatic

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/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 source_t >
static source_t xtd::linq::enumerable::aggregate ( const ienumerable< source_t > &  source,
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 
)
inlinestatic

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
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/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 result_t , class accumulate_t , class source_t >
static result_t xtd::linq::enumerable::aggregate ( const ienumerable< source_t > &  source,
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 
)
inlinestatic

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/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 source_t >
static bool xtd::linq::enumerable::all ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &)> &  predicate 
)
inlinestatic

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/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 source_t >
static bool xtd::linq::enumerable::any ( const ienumerable< source_t > &  source)
inlinestaticnoexcept

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/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::collections::generic::list< type_t > list
Represents the list value type.
Definition enumerable.hpp:77
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16

◆ any() [2/2]

template<class source_t >
static bool xtd::linq::enumerable::any ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &)> &  predicate 
)
inlinestatic

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/array>
#include <xtd/console>
using namespace xtd;
auto main() -> int {
struct pet : public object {
pet() = default;
pet(const string& name, int age) : name {name}, age {age} {}
string name;
int age = 0;
};
// Create an array of pets.
auto pets = array {
pet {"Barley", 10},
pet {"Boots", 4},
pet {"Whiskers", 6}
};
// Determine whether all pet names in the array start with 'B'.
bool all_start_with_b = pets.all([](const pet& pet) {
return pet.name.starts_with("B");
});
console::write_line("{} pet names start with 'B'.", all_start_with_b ? "All" : "Not all");
}
// This code produces the following output :
//
// Not all pet names start with 'B'.

◆ append()

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::append ( const ienumerable< source_t > &  source,
const source_t &  element 
)
inlinestaticnoexcept

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/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() [1/6]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::as_enumerable ( const ienumerable< source_t > &  source)
inlinestaticnoexcept

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/6]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::as_enumerable ( std::initializer_list< source_t >  source)
inlinestaticnoexcept

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/6]

template<class collection_t >
static const auto & xtd::linq::enumerable::as_enumerable ( const collection_t &  source)
inlinestaticnoexcept

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>.
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/6]

template<class input_iterator_t >
static const auto & xtd::linq::enumerable::as_enumerable ( input_iterator_t  first,
input_iterator_t  last 
)
inlinestaticnoexcept

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() [5/6]

template<class input_iterator_t >
static const auto & xtd::linq::enumerable::as_enumerable ( input_iterator_t  iterator,
size_t  length 
)
inlinestaticnoexcept

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() [6/6]

template<class source_t , size_t length>
static const auto & xtd::linq::enumerable::as_enumerable ( const source_t(&)  array[length])
inlinestaticnoexcept

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]

static xtd::decimal xtd::linq::enumerable::average ( const ienumerable< xtd::decimal > &  source)
static

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]

static double xtd::linq::enumerable::average ( const ienumerable< double > &  source)
static

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]

static float xtd::linq::enumerable::average ( const ienumerable< float > &  source)
static

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]

static double xtd::linq::enumerable::average ( const ienumerable< xtd::int32 > &  source)
static

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]

static double xtd::linq::enumerable::average ( const ienumerable< xtd::int64 > &  source)
static

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]

static xtd::optional< xtd::decimal > xtd::linq::enumerable::average ( const ienumerable< xtd::optional< xtd::decimal > > &  source)
staticnoexcept

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]

static xtd::optional< double > xtd::linq::enumerable::average ( const ienumerable< xtd::optional< double > > &  source)
staticnoexcept

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]

static xtd::optional< float > xtd::linq::enumerable::average ( const ienumerable< xtd::optional< float > > &  source)
staticnoexcept

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]

static xtd::optional< double > xtd::linq::enumerable::average ( const ienumerable< xtd::optional< xtd::int32 > > &  source)
staticnoexcept

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]

static xtd::optional< double > xtd::linq::enumerable::average ( const ienumerable< xtd::optional< xtd::int64 > > &  source)
staticnoexcept

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<class result_t , class source_t >
static const ienumerable< result_t > & xtd::linq::enumerable::cast ( const ienumerable< source_t > &  source)
staticnoexcept

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<class source_t >
static const ienumerable< xtd::array< source_t > > & xtd::linq::enumerable::chunk ( const ienumerable< source_t > &  source,
xtd::size  size 
)
static

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<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::concat ( const ienumerable< source_t > &  first,
const ienumerable< source_t > &  second 
)
inlinestaticnoexcept

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/2]

template<class source_t >
static bool xtd::linq::enumerable::contains ( const ienumerable< source_t > &  source,
const source_t &  value 
)
inlinestaticnoexcept

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/2]

template<class source_t >
static bool xtd::linq::enumerable::contains ( const ienumerable< source_t > &  source,
const source_t &  value,
const xtd::collections::generic::iequality_comparer< source_t > &  comparer 
)
inlinestaticnoexcept

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.

◆ count() [1/3]

template<class source_t >
static xtd::size xtd::linq::enumerable::count ( const ienumerable< source_t > &  source)
inlinestaticnoexcept

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>
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 source_t >
static xtd::size xtd::linq::enumerable::count ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &)> &  predicate 
)
inlinestaticnoexcept

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>
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 source_t >
static xtd::size xtd::linq::enumerable::count ( const ienumerable< source_t > &  source,
const source_t &  value 
)
inlinestaticnoexcept

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/2]

template<class key_t , class source_t >
static const ienumerable< key_value_pair< key_t, xtd::size > > & xtd::linq::enumerable::count_by ( const ienumerable< source_t > &  source,
const std::function< key_t(const source_t &)> &  key_selector 
)
inlinestaticnoexcept

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>
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
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
static xtd::size count(const ienumerable< source_t > &source) noexcept
Returns the number of elements in a sequence.
Definition enumerable.hpp:474
@ query
The xtd::uri::query data.
@ other
The operating system is other.

◆ count_by() [2/2]

template<class key_t , class source_t >
static const ienumerable< key_value_pair< key_t, xtd::size > > & xtd::linq::enumerable::count_by ( const ienumerable< source_t > &  source,
const std::function< key_t(const source_t &)> &  key_selector,
const iequality_comparer< key_t > &  key_comparer 
)
inlinestaticnoexcept

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>
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 source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::default_if_empty ( const ienumerable< source_t > &  source)
inlinestaticnoexcept

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>
using namespace 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);
auto pets2 = array<pet> {};
for (const auto& [name, age] : pets2.default_if_empty())
console::write_line("pet {{name={}, age={}}}", name, age);
}
// 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}
//
static const ienumerable< source_t > & 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 c...
Definition enumerable.hpp:556
xtd::string name() noexcept
Gets the thread name of the current thread.

◆ default_if_empty() [2/2]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::default_if_empty ( const ienumerable< source_t > &  source,
const source_t &  default_value 
)
inlinestaticnoexcept

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>
using namespace 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);
auto pets2 = array<pet> {};
for (const auto& [name, age] : pets2.default_if_empty(pet::default_pet))
console::write_line("pet {{name={}, age={}}}", name, age);
}
// 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}
//

◆ first_or_default() [1/4]

template<class source_t >
static source_t xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &)> &  predicate,
const source_t &  default_value 
)
inlinestaticnoexcept

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<class source_t >
static source_t xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &)> &  predicate 
)
inlinestaticnoexcept

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<class source_t >
static source_t xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > &  source,
const source_t &  default_value 
)
inlinestaticnoexcept

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<class source_t >
static source_t xtd::linq::enumerable::first_or_default ( const ienumerable< source_t > &  source)
inlinestaticnoexcept

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.

◆ range() [1/3]

template<class type_t >
static const ienumerable< type_t > & xtd::linq::enumerable::range ( type_t  count)
inlinestatic

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/linq/enumerable>
#include <xtd/console>
using namespace xtd;
using namespace xtd::linq;
auto main() -> int {
const auto& squares = enumerable::range(1, 10).select([](int x) {return x * x;});
for (auto num : squares)
console::write_line(num);
}
// This code produces the following output :
//
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
// 100
static const ienumerable< type_t > & range(type_t count)
Generates a sequence of integral numbers within a specified range.
Definition enumerable.hpp:624
@ x
The X key.
Provides classes and interfaces that support queries that use Language-Integrated Query (LINQ).
Definition enumerable.hpp:41

◆ range() [2/3]

template<class type_t >
static const ienumerable< type_t > & xtd::linq::enumerable::range ( type_t  start,
type_t  count 
)
inlinestatic

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/linq/enumerable>
#include <xtd/console>
using namespace xtd;
using namespace xtd::linq;
auto main() -> int {
const auto& squares = enumerable::range(1, 10).select([](int x) {return x * x;});
for (auto num : squares)
console::write_line(num);
}
// This code produces the following output :
//
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
// 100

◆ range() [3/3]

template<class type_t >
static const ienumerable< type_t > & xtd::linq::enumerable::range ( type_t  start,
type_t  count,
type_t  step 
)
inlinestatic

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.

◆ from() [1/6]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::from ( const ienumerable< source_t > &  source)
inlinestaticnoexcept

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>
using namespace xtd;
using namespace xtd::linq;
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
typename xtd::collections::generic::enumerator< type_t > enumerator
Represents the enumerator value type.
Definition enumerable.hpp:65
static const ienumerable< source_t > & from(const ienumerable< source_t > &source) noexcept
Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
Definition enumerable.hpp:681

◆ from() [2/6]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::from ( std::initializer_list< source_t >  source)
inlinestaticnoexcept

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>
using namespace xtd;
using namespace xtd::linq;
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<class collection_t >
static const auto & xtd::linq::enumerable::from ( const collection_t &  source)
inlinestaticnoexcept

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>
using namespace xtd;
using namespace xtd::linq;
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<class input_iterator_t >
static const auto & xtd::linq::enumerable::from ( input_iterator_t  first,
input_iterator_t  last 
)
inlinestaticnoexcept

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>
using namespace xtd;
using namespace xtd::linq;
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<class input_iterator_t >
static const auto & xtd::linq::enumerable::from ( input_iterator_t  iterator,
size_t  length 
)
inlinestaticnoexcept

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, size_t length).
Examples
The following code example demonstrates how to use xtd::linq::from to create a sequence of values.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::linq;
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<class source_t , size_t length>
static const auto & xtd::linq::enumerable::from ( const source_t(&)  array[length])
inlinestaticnoexcept

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>
using namespace xtd;
using namespace xtd::linq;
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

◆ select() [1/4]

template<class result_t , class source_t >
static const ienumerable< result_t > & xtd::linq::enumerable::select ( const ienumerable< source_t > &  source,
const std::function< result_t(const source_t &)> &  selector 
)
inlinestatic

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/linq/enumerable>
#include <xtd/console>
using namespace xtd;
using namespace xtd::linq;
auto main() -> int {
const auto& squares = enumerable::range(1, 10).select([](int x) {return x * x;});
for (auto num : squares)
console::write_line(num);
}
// This code produces the following output :
//
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
// 100

◆ select() [2/4]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::select ( const ienumerable< source_t > &  source,
const std::function< source_t(const source_t &)> &  selector 
)
inlinestatic

Projects each element of a sequence into a new form.

Template Parameters
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/linq/enumerable>
#include <xtd/console>
using namespace xtd;
using namespace xtd::linq;
auto main() -> int {
const auto& squares = enumerable::range(1, 10).select([](int x) {return x * x;});
for (auto num : squares)
console::write_line(num);
}
// This code produces the following output :
//
// 1
// 4
// 9
// 16
// 25
// 36
// 49
// 64
// 81
// 100

◆ select() [3/4]

template<class result_t , class source_t >
static const ienumerable< result_t > & xtd::linq::enumerable::select ( const ienumerable< source_t > &  source,
const std::function< result_t(const source_t &, xtd::size)> &  selector 
)
inlinestatic

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::size)>&) 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 source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::select ( const ienumerable< source_t > &  source,
const std::function< source_t(const source_t &, xtd::size)> &  selector 
)
inlinestatic

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

Template Parameters
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::size)>&) 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 source_t >
static const list< source_t > & xtd::linq::enumerable::to_list ( const ienumerable< source_t > &  source)
staticnoexcept

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/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
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
static const list< source_t > & to_list(const ienumerable< source_t > &source) noexcept
Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <ty...

◆ where() [1/2]

template<class source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::where ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &)> &  predicate 
)
inlinestatic

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>
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 source_t >
static const ienumerable< source_t > & xtd::linq::enumerable::where ( const ienumerable< source_t > &  source,
const std::function< bool(const source_t &, xtd::size)> &  predicate 
)
inlinestatic

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

Template Parameters
source_tThe type of the elements of source.
Parameters
sourceA sequence of values to filter.
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&, xtd::size)>&) 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: