diff --git a/CPP17.md b/CPP17.md index a4b2492..07e3189 100644 --- a/CPP17.md +++ b/CPP17.md @@ -560,6 +560,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6 // Using a custom binary op: std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6 ``` +Additionally you can specify transformations for reducers: +```c++ +std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60 + +const std::array b{ 1, 2, 3 }; +const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; }; + +std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140 +``` ### Prefix sum algorithms Support for prefix sums (both inclusive and exclusive scans) along with transformations. diff --git a/README.md b/README.md index db13925..90f33cc 100644 --- a/README.md +++ b/README.md @@ -1224,6 +1224,15 @@ std::reduce(std::cbegin(a), std::cend(a)); // == 6 // Using a custom binary op: std::reduce(std::cbegin(a), std::cend(a), 1, std::multiplies<>{}); // == 6 ``` +Additionally you can specify transformations for reducers: +```c++ +std::transform_reduce(std::cbegin(a), std::cend(a), 0, std::plus<>{}, times_ten); // == 60 + +const std::array b{ 1, 2, 3 }; +const auto product_times_ten = [](const auto a, const auto b) { return a * b * 10; }; + +std::transform_reduce(std::cbegin(a), std::cend(a), std::cbegin(b), 0, std::plus<>{}, product_times_ten); // == 140 +``` ### Prefix sum algorithms Support for prefix sums (both inclusive and exclusive scans) along with transformations.