#pragma once #include #include namespace c10 { // The passed in function must take T by value (T), or by // const reference (const T&); taking T by non-const reference // will result in an error like: // // error: no type named 'type' in 'class std::result_of' // // No explicit template parameters are required. // Overload for explicit function and ArrayRef template inline auto fmap(const T& inputs, const F& fn) -> std::vector { std::vector r; r.reserve(inputs.size()); for(const auto & input : inputs) r.push_back(fn(input)); return r; } // C++ forbids taking an address of a constructor, so here's a workaround... // Overload for constructor (R) application template inline std::vector fmap(const T& inputs) { std::vector r; r.reserve(inputs.size()); for(auto & input : inputs) r.push_back(R(input)); return r; } template inline std::vector filter(at::ArrayRef inputs, const F& fn) { std::vector r; r.reserve(inputs.size()); for(auto & input : inputs) { if (fn(input)) { r.push_back(input); } } return r; } template inline std::vector filter(const std::vector& inputs, const F& fn) { return filter(static_cast>(inputs), fn); } } // namespace c10