std::valarray::apply(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::valarray::apply β€” the C++ Standard Library glow-up πŸ’–

okay so bestie, this is the <std::valarray::apply(3)> page from the C++ Standard Library, version 2024.06.10 β€” and we're about to make it make sense, no cap. ✨


🌸 NAME 🌸

std::valarray::apply β€” literally just std::valarray::apply, she's iconic like that πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

valarray<T> apply( T func(T) ) const;
valarray<T> apply( T func(const T&) ) const;

Returns a brand-new valarray, same size as the original, where every value has been acquired by applying the function func to the previous values of the elements. it's giving transformation era, fr fr πŸ”₯


πŸŽ€ PARAMETERS πŸŽ€

  • func β€” the function you're applying to the values. that's the whole vibe check right there 🫢

⭐ RETURN VALUE ⭐

The resulting valarray, with values acquired by applying the function func. simple as that, no notes 😭


πŸ’€ NOTES πŸ’€

okay so here's where it gets a little spicy β€” the function can be implemented with a return type that's different from std::valarray. not me explaining the plot twist πŸ™ˆ

In that case, the replacement type has the following properties (yes, all of them, we don't skip):

  • All const member functions of std::valarray are provided.
  • std::valarray, std::slice_array, std::gslice_array, std::mask_array and std::indirect_array can be constructed from the replacement type.
  • For every function taking a const std::valarray<T>& except begin() and end() (since C++11), identical functions taking the replacement types shall be added;
  • For every function taking two const std::valarray<T>& arguments, identical functions taking every combination of const std::valarray<T>& and replacement types shall be added.
  • The return type does not add more than two levels of template nesting over the most deeply-nested argument type.
bestie, if you're implementing a replacement type, these bullet points are not optional. read them twice. πŸ’œ

πŸ”₯ Possible implementation πŸ”₯

Following straightforward implementations can be replaced by expression templates for a higher efficiency β€” iykyk, that's the efficiency glow-up ✨

template<class T>
valarray<T> valarray<T>::apply(T func(T)) const
{
    valarray<T> other = *this;
    for (T& i : other)
        i = func(i);
    return other;
}

template<class T>
valarray<T> valarray<T>::apply(T func(const T&)) const
{
    valarray<T> other = *this;
    for (T& i : other)
        i = func(i);
    return other;
}

πŸ“ Example πŸ“

Calculates and prints the first 10 factorials β€” and honestly? the numbers ate. πŸ’…

// Run this code

#include <cmath>
#include <iostream>
#include <valarray>

int main()
{
    std::valarray<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    v = v.apply([](int n) -> int
                {
                    return std::round(std::tgamma(n + 1));
                });
    for (auto n : v)
        std::cout << n << ' ';
    std::cout << '\n';
}

Output:

1 2 6 24 120 720 5040 40320 362880 3628800

look at those factorials go 😭πŸ”₯


🌈 See also 🌈

  • for_each β€” applies a function to a range of elements (function template)
  • ranges::for_each (C++20) β€” applies a function to a range of elements (niebloid)

Hidden category: πŸ’€ - Pages with unreviewed LWG DR marker

and that's the whole page, bestie β€” no skips, no summaries, every detail present and accounted for 🫢✨

this has been <std::valarray::apply(3)> from the C++ Standard Library, 2024.06.10 β€” go forth and apply your functions, ily, mwah πŸ’–πŸŽ€πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from