Skip to content

Commit

Permalink
Add select methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Dec 26, 2024
1 parent 6ede2e7 commit 3835340
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ namespace xtd {
return xtd::linq::enumerable::average(base());
}

/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename result_t>
const xtd::collections::generic::ienumerable<result_t>& select(const std::function<result_t(const source_t&)>& selector) const {
return xtd::linq::enumerable::select<source_t, result_t>(base(), selector);
}

/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename result_t, typename selector_t>
const xtd::collections::generic::ienumerable<result_t>& select(const selector_t& selector) const {
return xtd::linq::enumerable::select<source_t, result_t>(base(), selector);
}

/// @brief Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
/// @return A xtd::collections::generic::list <type_t> that contains elements from the input sequence.
const xtd::collections::generic::list<source_t>& to_list() const noexcept;
Expand Down
84 changes: 84 additions & 0 deletions src/xtd.core/include/xtd/linq/enumerable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,90 @@ namespace xtd {
/// @exception xtd::argument_out_of_range_exception `count` is less than 0.
static const xtd::collections::generic::ienumerable<xtd::int32>& range(xtd::int32 start, xtd::int32 count);

/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename source_t, typename result_t>
static const xtd::collections::generic::ienumerable<result_t>& select(const xtd::collections::generic::ienumerable<source_t>& source, const std::function<result_t(const source_t&)>& selector) {
static thread_local auto result = enumerable_collection<result_t> {};
result = enumerable_collection<result_t> {};
for (const auto& item : source)
result.items.push_back(selector(item));
return result;
}
/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename source_t, typename result_t>
static const xtd::collections::generic::ienumerable<result_t>& select(const std::initializer_list<source_t>& source, const std::function<result_t(const source_t&)>& selector) {
static thread_local auto result = enumerable_collection<result_t> {};
result = enumerable_collection<result_t> {};
for (const auto& item : source)
result.items.push_back(selector(item));
return result;
}
/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename source_t, typename result_t, typename collection_t>
static const xtd::collections::generic::ienumerable<result_t>& select(const collection_t& source, const std::function<result_t(const source_t&)>& selector) {
static thread_local auto result = enumerable_collection<result_t> {};
result = enumerable_collection<result_t> {};
for (const auto& item : source)
result.items.push_back(selector(item));
return result;
}
/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename source_t, typename result_t, typename input_iterator_t>
static const xtd::collections::generic::ienumerable<result_t>& select(input_iterator_t first, input_iterator_t last, const std::function<result_t(const source_t&)>& selector) {
static thread_local auto result = enumerable_collection<result_t> {};
result = enumerable_collection<result_t> {};
for (auto iterator = first; iterator != last; ++iterator)
result.items.push_back(selector(*iterator));
return result;
}

/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename source_t, typename result_t, typename selector_t>
static const xtd::collections::generic::ienumerable<result_t>& select(const xtd::collections::generic::ienumerable<source_t>& source, const selector_t& selector) {
return select(source, std::function<result_t(const source_t&)> {selector});
}
/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename source_t, typename result_t, typename selector_t>
static const xtd::collections::generic::ienumerable<result_t>& select(const std::initializer_list<source_t>& source, const selector_t& selector) {
return select(source, std::function<result_t(const source_t&)> {selector});
}
/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename result_t, typename collection_t, typename selector_t>
static const xtd::collections::generic::ienumerable<result_t>& select(const collection_t& source, const selector_t& selector) {
using source_t = typename collection_t::value_type;
return select(source, std::function<result_t(const source_t&)> {selector});
}
/// @brief Projects each element of a sequence into a new form.
/// @param source A sequence of values to invoke a transform function on.
/// @param selector A transform function to apply to each element.
/// @return An xtd::collections::generic::ienumerable <type_t> whose elements are the result of invoking the transform function on each element of source.
template<typename result_t, typename input_iterator_t, typename selector_t>
static const xtd::collections::generic::ienumerable<result_t>& select(input_iterator_t first, input_iterator_t last, const selector_t& selector) {
using source_t = typename std::decay<decltype(*first)>::type;
return select(first, last, std::function<result_t(const source_t&)> {selector});
}

/// @brief Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <type_t>.
/// @param source The xtd::collections::generic::ienumerable <type_t> to create a xtd::collections::generic::list <type_t> from.
/// @return A xtd::collections::generic::list <type_t> that contains elements from the input sequence.
Expand Down

0 comments on commit 3835340

Please sign in to comment.