Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-69675

Simplify window_function_exec_linear_fill.cpp's interpolate function

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.2.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible
    • Service Arch 2022-09-19

      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));
      }
      

            Assignee:
            billy.donahue@mongodb.com Billy Donahue
            Reporter:
            billy.donahue@mongodb.com Billy Donahue
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: