From d096eb9d2155b8e678924566af1b841e6bc28f97 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 9 Feb 2022 12:51:02 +1000 Subject: [PATCH] tupe/value: add transform_array --- tuple/value.hpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tuple/value.hpp b/tuple/value.hpp index 424920ba..c037158b 100644 --- a/tuple/value.hpp +++ b/tuple/value.hpp @@ -245,4 +245,45 @@ namespace cruft::tuple::value { std::forward (val) ); } + + + /////////////////////////////////////////////////////////////////////////// + namespace detail { + template + requires ( + std::is_convertible_v< + std::tuple_element_t, + std::common_type< + std::tuple_element_t... + > + > && ... + ) + auto + transform_array (index::indices, FunctionT &&func, TupleT &&val) + { + using value_type = std::common_type< + std::tuple_element_t... + >; + + return std::array> ( + std::invoke (func, std::get (val))... + ); + } + } + + + ///------------------------------------------------------------------------ + /// Transform each element of a tuple with the supplied function and + /// return an array of the return values. + /// + /// The function must return types that have a computable common type. + template + auto + transform_array (FunctionT &&func, TupleT &&val) + { + return transform_array ( + std::forward (func), + std::forward (val) + ); + } }