iterate(x, f)でx, f(x), f(f(x)), ...と出します。unfoldでも実現可能だけど、めんどうなので。
template<typename T, typename U> class cIterate : public cIterable<T> { T current; U func; bool first; public: cIterate(T x, U f) : current(x), func(f), first(true) { } bool exists_next() { if(first) first = false; else current = func(current); return true; } T value() const { return current; } }; template<typename T, typename U> shared_ptr<cIterable<T>> iterate(T x, U f) { return shared_ptr<cIterable<T>>(new cIterate<T,U>(x, f)); } int main() { cout << takewhile([] (int n) { return n < 100; }, iterate(1, [](int n) { return n * 2; })) << endl; }
1 2 4 8 16 32 64