#include <iostream>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
template <template <class...> class>
struct monad;
template <template <class...> class M, typename A, typename F>
auto operator >>=(M<A> const & x, F f) -> decltype(declval<F>()(declval<A>())) {
return monad<M>::bind(x, f);
};
template <template <class...> class M, typename T>
auto return_(T x) -> M<T> {
return monad<M>::return_(x);
}
template <>
struct monad<vector> {
template <typename A, typename F>
static auto bind(vector<A> const & x, F f) -> decltype(declval<F>()(declval<A>())) {
typedef decltype(declval<F>()(declval<A>())) vector_B;
vector_B ret;
for (typename vector<A>::const_iterator it = x.begin(), iend = x.end(); it != iend; ++it) {
vector_B r = f(*it);
copy(r.begin(), r.end(), back_inserter(ret));
}
return ret;
}
template <typename T>
static auto return_(T x) -> vector<T> {
return vector<T>(1, x);
}
};
int main() {
vector<int> v = {1, 2, 3}, w = {1, 2, 3};
vector<int> r = v >>= [&](int x) { return w >>= [&](int y) { return return_<vector, int>(x+y); }; };
copy(r.begin(), r.end(), ostream_iterator<int>(cout, " "));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGl0ZXJhdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdGVtcGxhdGUgPHRlbXBsYXRlIDxjbGFzcy4uLj4gY2xhc3M+CnN0cnVjdCBtb25hZDsKdGVtcGxhdGUgPHRlbXBsYXRlIDxjbGFzcy4uLj4gY2xhc3MgTSwgdHlwZW5hbWUgQSwgdHlwZW5hbWUgRj4KYXV0byBvcGVyYXRvciA+Pj0oTTxBPiBjb25zdCAmIHgsIEYgZikgLT4gZGVjbHR5cGUoZGVjbHZhbDxGPigpKGRlY2x2YWw8QT4oKSkpIHsKCXJldHVybiBtb25hZDxNPjo6YmluZCh4LCBmKTsKfTsKdGVtcGxhdGUgPHRlbXBsYXRlIDxjbGFzcy4uLj4gY2xhc3MgTSwgdHlwZW5hbWUgVD4KYXV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