This PR replaces a raw for loop with an algorithm. In principle, you should not use raw for loops. Preferably replace them with an algorithm or at least a range-based for loop. This all to express intent: indicate what should be done rather than just how it should be done.
Sometimes (like in this case), the standard (or Boost) has no suitable algorithm. I then prefer to write my own.
I realize this change might be controversial but at the very least I wanted to show you how this can be done.
Note the following:
instead of a raw for loop we now have a named algorithm call transform_if() which has its own documentation. If you're familiar with std::transform() or the std algorithms in general, the meaning should already be clear.
we now use const iterators so we know the algorithm can't change anything in the source container
it's a lot easier to check if we stay within the bounds of the source container
both the if and transform predicates are lambdas which mostly forward to named functions, so it is clear what they do as well
this particular change resulted in more lines of code because the algorithm had to be added (and documented). once you have your own library of algorithms (in addition to the standard ones) this will typically not be the case.
This PR replaces a raw for loop with an algorithm. In principle, you should not use raw for loops. Preferably replace them with an algorithm or at least a range-based for loop. This all to express intent: indicate what should be done rather than just how it should be done.
Sometimes (like in this case), the standard (or Boost) has no suitable algorithm. I then prefer to write my own.
I realize this change might be controversial but at the very least I wanted to show you how this can be done.
Note the following:
transform_if()
which has its own documentation. If you're familiar withstd::transform()
or the std algorithms in general, the meaning should already be clear.