-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapreduce.h
74 lines (62 loc) · 1.86 KB
/
mapreduce.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2018 Anders Dalle Henning Dalvander
//
// Use, modification and distribution are subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
#if !defined(ADHD_MAPREDUCE_H)
#define ADHD_MAPREDUCE_H
#include <numeric>
#include <utility>
#include <vector>
namespace adhd {
template <typename TMap>
struct map_t {
TMap map;
};
template <typename TMap>
map_t<TMap> map(TMap map) {
return { std::move(map) };
}
template <typename T, typename TMap>
auto operator>>(const std::vector<T>& lhs, const map_t<TMap>& rhs) {
using TMapped = decltype(rhs.map(std::declval<T>()));
std::vector<TMapped> m;
m.reserve(lhs.size());
for (const auto& e : lhs) {
m.emplace_back(rhs.map(e));
}
return std::move(m);
}
template <typename TFilter>
struct filter_t {
TFilter filter;
};
template <typename TFilter>
filter_t<TFilter> filter(TFilter filter) {
return { std::move(filter) };
}
template <typename T, typename TFilter>
auto operator>>(const std::vector<T>& lhs, const filter_t<TFilter>& rhs) {
std::vector<T> m;
for (const auto& e : lhs) {
if (rhs.filter(e)) {
m.emplace_back(e);
}
}
return std::move(m);
}
template <typename TReduce, typename TInit>
struct reduce_t {
TReduce reduce;
TInit init;
};
template <typename TReduce, typename TInit>
reduce_t<TReduce, TInit> reduce(TReduce reduce, TInit init) {
return { std::move(reduce), std::move(init) };
}
template <typename T, typename TReduce, typename TInit>
auto operator>>(const std::vector<T>& lhs, const reduce_t<TReduce, TInit>& rhs) {
return std::reduce(std::begin(lhs), std::end(lhs), rhs.init, rhs.reduce);
}
}
#endif