Skip to content

Commit 8bd90cb

Browse files
committed
refactor: make use of std::expected chaining
1 parent 3f0233b commit 8bd90cb

1 file changed

Lines changed: 8 additions & 19 deletions

File tree

include/lfmc/pipeline/pipeline.hpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,14 @@ template <StochasticProcess SP, NumericalScheme<SP> NS> class Pipeline {
2626

2727
std::expected<double, std::string> run(size_t steps, double T) {
2828
while (!estimator_->converged()) {
29-
auto normals = random_source_->generate_normals(steps);
30-
if (!normals) {
31-
return std::unexpected("Failed to generate random normals");
32-
}
33-
34-
auto paths = path_generator_->generate_paths(normals.value(), steps, T);
35-
if (!paths) {
36-
return std::unexpected("Failed to generate paths");
37-
}
38-
39-
auto payoffs = payoff_->generate_payoffs(paths.value());
40-
if (!payoffs) {
41-
return std::unexpected("Failed to generate payoffs");
42-
}
43-
44-
auto result = estimator_->add_payoffs(payoffs.value());
45-
if (!result) {
46-
return std::unexpected(result.error());
47-
}
29+
auto result =
30+
random_source_->generate_normals(steps)
31+
.and_then([&](auto normals) {
32+
return path_generator_->generate_paths(normals, steps, T);
33+
})
34+
.and_then([&](auto paths) { return payoff_->generate_payoffs(paths); })
35+
.and_then([&](auto payoffs) { return estimator_->add_payoffs(payoffs); })
36+
.or_else([](auto error) { return std::unexpected(error); });
4837
}
4938

5039
return estimator_->result();

0 commit comments

Comments
 (0)