forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional.h
54 lines (46 loc) · 1.43 KB
/
functional.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <vector>
#include <c10/util/ArrayRef.h>
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<foobar::__lambda(T)>'
//
// No explicit template parameters are required.
// Overload for explicit function and ArrayRef
template<class F, class T>
inline auto fmap(const T& inputs, const F& fn) -> std::vector<decltype(fn(*inputs.begin()))> {
std::vector<decltype(fn(*inputs.begin()))> 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<typename R, typename T>
inline std::vector<R> fmap(const T& inputs) {
std::vector<R> r;
r.reserve(inputs.size());
for(auto & input : inputs)
r.push_back(R(input));
return r;
}
template<typename F, typename T>
inline std::vector<T> filter(at::ArrayRef<T> inputs, const F& fn) {
std::vector<T> r;
r.reserve(inputs.size());
for(auto & input : inputs) {
if (fn(input)) {
r.push_back(input);
}
}
return r;
}
template<typename F, typename T>
inline std::vector<T> filter(const std::vector<T>& inputs, const F& fn) {
return filter<F, T>(static_cast<at::ArrayRef<T>>(inputs), fn);
}
} // namespace c10