language: C++ 4.7.2 (gcc-4.7.2)
date: 819 days 11 hours ago
link:
visibility: public
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
 
#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/shared_ptr.hpp>
 
 
template <typename T>
struct identity
{
        T operator()(const T& x) { return x; }
};
 
struct any_function
{
        template <typename Res, typename Arg>
        any_function(boost::function<Res(Arg)> f)
        {
                impl.reset(make_impl(f));
        }
        
        boost::any operator()(const boost::any& x)
        {
                return impl->invoke(x);
        }
        
        static any_function compose(const any_function& f,
                                    const any_function& g)
        {
                any_function ans;
                ans.impl.reset(compose_impl(f.impl, g.impl));
                return ans;
        }
        
        template <typename T>
        static any_function id()
        {
                using boost::function;
                return function<T(T)>(identity<T>());
        }
        
        template <typename Res, typename Arg>
        boost::function<Res(Arg)> to_function()
        {
                return to_function_helper<Res, Arg>(impl);
        }
    
private:
        any_function() {}
        
        struct impl_type
        {
                virtual ~impl_type() {}
                virtual boost::any invoke(const boost::any&) = 0;
        };
        
        boost::shared_ptr<impl_type> impl;
        
        template <typename Res, typename Arg>
        static impl_type* make_impl(boost::function<Res(Arg)> f)
        {
                using boost::function;
                using boost::any;
                using boost::any_cast;
                
                class impl : public impl_type
                {
                        function<Res(Arg)> f;
                        
                        any invoke(const any& x)
                        {
                                return f(any_cast<Arg>(x));
                        }
                        
                public:
                        impl(function<Res(Arg)> f) : f(f) {}
                };
        
                return new impl(f);
        }
        
        static impl_type* compose_impl(const boost::shared_ptr<impl_type>& f,
                                       const boost::shared_ptr<impl_type>& g)
        {
                using boost::any;
                using boost::shared_ptr;
                
                class impl : public impl_type
                {
                        shared_ptr<impl_type> f, g;
                        
                        any invoke(const any& x)
                        {
                                return g->invoke(f->invoke(x));
                        }
                        
                public:
                        impl(const shared_ptr<impl_type>& f,
                             const shared_ptr<impl_type>& g)
                                : f(f), g(g)
                        {}
                };
                
                return new impl(f, g);
        }
        
        template <typename Res, typename Arg>
        struct to_function_helper
        {
                Res operator()(const Arg& x)
                {
                        using boost::any;
                        using boost::any_cast;
                        
                        return any_cast<Res>(p->invoke(any(x)));
                }
                
                to_function_helper(const boost::shared_ptr<impl_type>& p) : p(p) {}
                
        private:
                boost::shared_ptr<impl_type> p;
        };
};
 
 
template <typename Arg, typename Res, typename I>
boost::function<Res(Arg)> pipeline(I begin, I end)
{
        any_function ans = std::accumulate(
                begin, end, 
                any_function::id<Arg>(),
                std::ptr_fun(any_function::compose)
                );
 
        return ans.to_function<Res, Arg>();
}
 
 
double f(double x)
{
        return 4.2 * x;
}
 
int g(double x)
{
        return static_cast<int>(x - 2);
}
 
 
int main()
{
        using boost::function;
 
        std::vector<double> v;
        v.push_back(2);
        v.push_back(-1);
 
        std::vector<any_function> funcs;
        funcs.push_back(function<double(double)>(f));
        funcs.push_back(function<int(double)>(g));
 
        std::transform(v.begin(), v.end(),
                       std::ostream_iterator<int>(std::cout, "\n"),
                       pipeline<double, int>(funcs.begin(), funcs.end())
                );
                
        typedef boost::transform_iterator<
                boost::function<int(double)>, 
                std::vector<double>::const_iterator
        > iterator;
 
    boost::function<int(double)> ff = pipeline<double, int>(funcs.begin(), funcs.end());
    std::copy(iterator(v.begin(), ff), iterator(v.end(), ff), std::ostream_iterator<int>(std::cout, "\n"));
}