|
I happened across this while considering replacing StatusWith::andThen.
This is the only code that uses it, and it's pretty gnarly code.
I have a much cleaner way to write this without StatusWith::andThen.
I'll send off as a PR.
mongo::Value interpolate(
|
mongo::Value x1, mongo::Value y1, mongo::Value x2, mongo::Value y2, mongo::Value xCoord) {
|
// Given two known points (x1, y1) and (x2, y2) and a value x that lies between those two
|
// points, we solve (or fill) for y with the following formula: y = y1 + (x - x1) * ((y2 -
|
// y1)/(x2 - x1))
|
return uassertStatusOK(mongo::ExpressionSubtract::apply(y2, y1).andThen([&](auto&& numerator) {
|
return mongo::ExpressionSubtract::apply(x2, x1).andThen([&](auto&& denominator) {
|
return mongo::ExpressionDivide::apply(numerator, denominator)
|
.andThen([&](auto&& quotient) {
|
return mongo::ExpressionSubtract::apply(xCoord, x1)
|
.andThen([&](auto&& difference) {
|
return mongo::ExpressionMultiply::apply(quotient, difference)
|
.andThen([&](auto&& product) {
|
return mongo::ExpressionAdd::apply(y1, product);
|
});
|
});
|
});
|
});
|
}));
|
Becomes (after defining operators in a private namespace):
Value interpolate(Value x1, Value y1, Value x2, Value y2, Value x) {
|
using namespace value_arithmetic_operators;
|
return y1 + (x - x1) * ((y2 - y1) / (x2 - x1));
|
}
|
|