-
Notifications
You must be signed in to change notification settings - Fork 75
Closed
Labels
Description
Suppose we have the following functions:
stlab::future<std::string> getFirstHitOnGoogle(std::string query); //Get the URL of the first hit on google for query
stlab::future<std::string> fetchUrl(std::string url); //Asynchronously load an URL
std::string extractJoke(std::string html); //Extract the joke from HTML inputTo get the best chuck norris joke, I'd like to be able to write something to the effect of this:
getFirstHitOnGoogle("chuck norris joke")
.then(fetchUrl)
.then(extractJoke)
.then([](const std::string& joke) {
std::cout << "Best Chuck Norris joke:" << joke << std::endl;
}).detach();But unfortunately, this will not work, as:
getFirstHitOnGoogle("chuck norris joke").then(fetchUrl)is of type stlab::future<stlab::future<std::string>>, and extractJoke is not of the form std::string extractJoke(stlab::future<std::string> html);.
I think at least a flatten function should be available of the following form:
template<typename T> stlab::future<T> flatten(stlab::future<stlab::future<T>> input);But ideally a specialized then_f would be even better:
template<typename A, typename B> stlab::future<B> stlab::future<A>::then_f(std::function<stlab::future<B>(const A&)> f);I suspect at least the flatten function should be rather trivial, and the specialized then/recover pair shouldn't be too hard either. Are there any plans to implement something like this ?