pitrou commented on a change in pull request #9643: URL: https://github.com/apache/arrow/pull/9643#discussion_r595159935
########## File path: cpp/src/arrow/util/vector.h ########## @@ -81,5 +84,53 @@ std::vector<T> FilterVector(std::vector<T> values, Predicate&& predicate) { return values; } +/// \brief Like MapVector, but where the function can fail. +template <typename Fn, typename From = internal::call_traits::argument_type<0, Fn>, + typename To = typename internal::call_traits::return_type<Fn>::ValueType> +Result<std::vector<To>> MaybeMapVector(Fn&& map, const std::vector<From>& src) { + std::vector<To> out; + out.reserve(src.size()); + ARROW_RETURN_NOT_OK( + MaybeTransform(src.begin(), src.end(), std::back_inserter(out), map)); + return out; +} + +template <typename Fn, typename From = internal::call_traits::argument_type<0, Fn>, + typename To = typename internal::call_traits::return_type<Fn>> +std::vector<To> MapVector(Fn&& map, const std::vector<From>& source) { + std::vector<To> out; + out.reserve(source.size()); + std::transform(source.begin(), source.end(), std::back_inserter(out), map); + return out; +} + +template <typename T> +std::vector<T> FlattenVectors(const std::vector<std::vector<T>>& vecs) { + std::size_t sum = 0; + for (const auto& vec : vecs) { + sum += vec.size(); + } + std::vector<T> out; + out.reserve(sum); + for (const auto& vec : vecs) { + out.insert(out.end(), vec.begin(), vec.end()); + } + return out; +} + +template <typename T> +Result<std::vector<T>> UnwrapOrRaise(std::vector<Result<T>> results) { Review comment: I think it's enough to take `std::vector<Result<T>>&& results`. No copy is done of the vector itself, we're just moving its individual elements. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org