Skip to content

Instantly share code, notes, and snippets.

@mnzk
Last active August 29, 2015 13:56
Show Gist options
  • Save mnzk/9346140 to your computer and use it in GitHub Desktop.
Save mnzk/9346140 to your computer and use it in GitHub Desktop.
Boost.Coroutine で itertoolsモドキ、その2
/*
* C++11 (VisualStudio 2013 Express Edition)
* boost 1.55.0
*/
#include <iostream>
#include <string>
#include <vector>
#include <boost/coroutine/all.hpp>
template<typename T>
class co_funcs{
typedef typename boost::coroutines::coroutine<T>::push_type push_type;
typedef typename boost::coroutines::coroutine<T>::pull_type pull_type;
typedef typename boost::coroutines::pull_coroutine<T> pull_coroutine;
co_funcs(){}
public:
static pull_coroutine drop(const int n, pull_coroutine& gen){
for (int i = 0; i < n; ++i) gen();
return std::move(gen);
}
/*
static pull_coroutine take(const int n, pull_coroutine& gen){
return pull_type([n, gen](push_type& yield){
for (int i = 0; i < n; ++i, g()) yield(gen.get());
});
}
*/
static pull_coroutine repeat(const T& value){
return pull_type([value](push_type& yield){
for (;;) yield(value);
});
}
static pull_coroutine cycle(const std::vector<T>& values){
return pull_type([values](push_type& yield){
for (;;){
for (auto& v : values) yield(v);
}
});
}
};
int main(){
typedef co_funcs<std::string> co;
auto cycle = co::cycle;
auto repeat = co::repeat;
auto drop = co::drop;
//auto take = co::take;
int i;
/*
i = 0;
for (auto& x : take(5, repeat("42"))){
std::cout << i << " : " << x << std::endl;
}
*/
i = 0;
for (auto& x : drop(2, cycle({ "A", "B", "C", "X", "Y", "Z" }))){
if (++i > 5) break;
std::cout << i << " : " << x << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment