Skip to content

Feature request: flatten futures or then_f. #95

@Frans-Willem

Description

@Frans-Willem

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 input

To 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 ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions