Skip to content

Instantly share code, notes, and snippets.

@RedBeard0531
Last active July 20, 2018 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedBeard0531/600d5389e1126d3ff395dbc87d07da3e to your computer and use it in GitHub Desktop.
Save RedBeard0531/600d5389e1126d3ff395dbc87d07da3e to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <boost/optional.hpp>
#include <string>
using boost::optional;
template <typename T>
struct Future{};
template <typename T>
struct SimpleBulkTask {
using Shape = size_t;
Shape shape();
void run(Shape s);
T finalize(); // Called once all runs have *returned*
};
template <typename T>
struct FancyBulkTask {
using Shape = size_t;
Shape shape();
Future<void> run(Shape s);
T finalize(); // Called once all runs' Futures are *completed*
};
template <typename T>
struct OddBulkTask {
using Shape = size_t;
Shape shape();
void run(Shape s);
Future<T> finalize(); // Called once all runs have *returned*
};
template <typename T>
struct FancyOddBulkTask {
using Shape = size_t;
Shape shape();
Future<void> run(Shape s);
Future<T> finalize(); // Called once all runs' Futures are *completed*
};
template < typename SF, typename RF, typename F>
auto makeBulkTask(SF shapeFactory, RF resultFactory, F run) {
using Result = decltype(resultFactory(shapeFactory()));
struct SomeBulkTask {
using Shape = decltype(shapeFactory);
SF shapeFactory;
RF resultFactory;
F runner;
optional<Result> result;
Shape shape() {
auto s = shapeFactory();
result.emplace(resultFactory(s));
return s;
}
auto run(Shape i) {
return runner(shape(), *result);
}
auto finalize() {
return std::move(*result);
}
};
return SomeBulkTask{
std::move(shapeFactory),
std::move(resultFactory),
std::move(run)
};
}
struct BulkExec {
// Returned Future is completed once the returned
template <typename T>
Future<T> exec(AnyBulkTask<T>&& task);
};
struct ExampleFancyOddTask {
// Fetches a week's worth of data, and sends it to a server for analysis.
using Shape = size_t;
Shape shape() { return 7; }
std::string data[7];
Future<void> run(Shape i) {
return get("http://source/day/" + std::to_string(i))
.then([this, i] (std::string res) { data[i] = std::move(res); });
}
Future<std::string> finalize() {
return post("http://analyze.my.data/", to_json(data));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment